Settlement automation document
Settlement automation
September 4, 2020 8:48 AM
********************* Cron job setup on TrueNAS ************************************
This cron job is set on TrueNAS file server /etc/crontab file as follows:
00 17 * * * root /usr/local/bin/python3 /root/snippets/upload_reports_to_onedrive.py > ~/cron.log 2>&1; mail -s "Settlement report files upload to OneDrive" helpdesk@companydomain.com
< /root/cron.log
********************* Sharepoint API setup using Microsoft Graph **********************************
To get your upload_to_onedrive.py working you'll have to follow this below steps:
1. login with settlement@companyname.com user on Azure portal (AAD - Azure Active Directory) and create a settlement_app API with following settings:
- go to AAD - App registrations - New registration
- pick a name - redirect URI (https://login.microsoftonline.com/common/oauth2/nativeclient) - select account type Multitenant
- Treat application as a public client - select Yes
- open your new app - Authentication and check all the above
- open API permissions and add 3 permissions Files.ReadWrite.All, Sites.ReadWrite.All and offline_access
- open Expose an API and create a scope. Add permissions above to your new scope.
2. build URL necessary to get the initial access code (see URL_to_call.txt) and using Internet Explorer open the URL and go to Developer Mode.
The response will give you in the browser a code="something".
3. Use POSTMAN form-data to POST a request to:
https://login.microsoftonline.com/common/oauth2/v2.0/token with the following body parameters:
grant_type = authorization_code
client_id = YourClientID
code = "the code you got above into your browser"
redirect_uri = https://login.microsoftonline.com/common/oauth2/nativeclient
this step will give you a refresh_token and an access_token
4. Use POSTMAN and above access_token to send a GET request to:
https://graph.microsoft.com/v1.0/me/drive/sharedWithMe and the response will give you the parent folder driveID and the Settlement item ID (see file response_from_POSTMAN.txt).
5. use the drive-id and item-id to create the upload URL for your code: URL="https://graph.microsoft.com/v1.0/me/drives/bVFsh4WaLIEGAfEE1zHjT22cI49MLvBFEq1wCtIXHONhJh8DyIAhIRpxrwYCBccXA/items/01IOYSCPKVUL3HYSYKHNHL764QWMF5AOKY:"
6. write the script
8:11 AM 2020-09-11
Updates - 2:51 PM 2021-03-03:
In case that your upload_reports_to_onedrive.py failed to run you need to follow the instructions above to generate a new refresh token and copy it to refresh_token file on the TrueNAS/FreeNAS /home/root/snippets.
cURL
September 4, 2020 8:48 AM
curl -X POST -d "grant_type=refresh_token&client_id=<your_client_id>&client_secret=<your_client_secret>&refres h_token=<your_refresh_token>" http://localhost:8000/o/token/
From <https://stackoverflow.com/questions/26903087/how-to-use-refresh-token-for-getting-new-access-tokendjango- oauth-toolkit>
URL to call to get initial authentication code
September 4, 2020 8:51 AM
URLs for authentication: ====================
https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=189e01b1-90ab-4604- bd8a-04bfdbd69cf8&response_type=code&redirect_uri=https%3A%2F% 2Flogin.microsoftonline.com%2Fcommon%2Foauth2% 2Fnativeclient&response_mode=query&scope=Sites.ReadWrite.All%20offline_access% 20Files.ReadWrite.All
Microsoft authorization app info
September 4, 2020 8:52 AM
Microsoft settlement API project: ======================
client ID: YourClientID
tenant ID: YourTenantID
client secret: YourClientSecret
redirect URI:
https://login.microsoftonline.com/common/oauth2/nativeclient
POSTMAN response
September 4, 2020 8:53 AM
Find parent folder driveID and itemID for Settlement shared folder. You'll have to use the initial access_token and do a POSTMAN GET request to: https://graph.microsoft.com/v1.0/me/drive/sharedWithMe
The response from POSTMAN for /me/drive/sharedWithMe is below: ==============================================================
{
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#Collection(driveItem)", "value": [{
"@odata.type": "#microsoft.graph.driveItem", "createdDateTime": "2020-06-30T14:55:18Z",
"id": "YourID", "lastModifiedDateTime": "2020-06-30T14:55:18Z", "name": "Settlements",
"webUrl": "https://CompanyName-my.sharepoint.com/personal/cnywening_gigadatsolutions_com/Documents/Settlements", "size": 0,
"createdBy": { "user": {
"email": "cnywening@Gigadatsolutions.com",
"displayName": "Cliff Nywening" }
}, "lastModifiedBy": {
"user": {
"email": "username@CompanyName.com", "displayName": "User Display Name"
} },
"fileSystemInfo": {
"createdDateTime": "2020-06-30T14:55:18Z", "lastModifiedDateTime": "2020-06-30T14:55:18Z"
}, "folder": {
"childCount": 62 },
"remoteItem": {
"createdDateTime": "2020-06-30T14:55:18Z",
"id": "YourID", "lastModifiedDateTime": "2020-06-30T14:55:18Z", "name": "Settlements",
"size": 0,
"webDavUrl": "https://CompanyName-my.sharepoint.com/personal/username_companyname_com/Documents/Settlements", "webUrl": "https://companyname-my.sharepoint.com/personal/username_companyname_com/Documents/Settlements", "createdBy": {
"user": {
"email": "username@CompanyName.com", "displayName": "User Display Name"
} },
"fileSystemInfo": {
"createdDateTime": "2020-06-30T14:55:18Z", "lastModifiedDateTime": "2020-06-30T14:55:18Z"
}, "folder": {
"childCount": 62 },
"lastModifiedBy": { "user": {
"email": "username@CompanyName.com",
"displayName": "User Display Name" }
}, "parentReference": {
"driveId": "YourDriveID",
"driveType": "business",
"id": "YourID" },
"shared": {
"scope": "users",
"sharedDateTime": "2020-07-10T20:37:29Z", "sharedBy": {
"user": {
"displayName": "Settlement User"
} }
}, "sharepointIds": {
"listId": "YourListID", "listItemId": "1760",
"listItemUniqueId": "YourListItemUniqueID", "siteId": "YourSiteID",
"siteUrl": "https://companyname- my.sharepoint.com/personal/username_companyname_com",
"tenantId": "YourTenantID",
"webId": "YourWebID" }
} } }