<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Jason</title>
    <description>The latest articles on DEV Community by Jason (@jsnmtr).</description>
    <link>https://dev.to/jsnmtr</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F182678%2F84c7a562-0fff-4d46-b5ac-bd3e703372d5.png</url>
      <title>DEV Community: Jason</title>
      <link>https://dev.to/jsnmtr</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jsnmtr"/>
    <language>en</language>
    <item>
      <title>Automating files upload to Microsoft OneDrive. Unexpected challenges and a success story!  </title>
      <dc:creator>Jason</dc:creator>
      <pubDate>Thu, 14 May 2020 20:35:47 +0000</pubDate>
      <link>https://dev.to/jsnmtr/automating-files-upload-to-microsoft-onedrive-unexpected-challenges-and-a-success-story-2ini</link>
      <guid>https://dev.to/jsnmtr/automating-files-upload-to-microsoft-onedrive-unexpected-challenges-and-a-success-story-2ini</guid>
      <description>&lt;p&gt;In the current data era we live in, a huge number of reports are being generated every minute. Uploading and distributing these reports is an inconvenience to say the least, and a security threat if sensitive/confidential data is included in these reports.&lt;/p&gt;

&lt;p&gt;I am currently in charge of approximately 200 analytics reports generated daily, attached to emails and sent to the according audience at different departments. While this is a time consuming task, it was not a security concern just because these emails and reports were accessed on company approved devices in a closed and highly secured network.&lt;/p&gt;

&lt;p&gt;Now with the pandemic and the stay in home orders. The chances of downloading these reports on unsecured machines became an urgent issue.&lt;/p&gt;

&lt;p&gt;So I decided to take advantage of Microsoft OneDrive as a central storage solution. Automating uploading these reports to a departmental folders and email just a link to the file location to my audience, reducing the chances of someone downloading the files on their machine when Microsoft OneDrive encourages web previews.&lt;/p&gt;

&lt;p&gt;This sounds like an easy task, right? Especially if you have worked with Python and APIs before. Include the requests library, configure your Client Id and Client secret, request an AccessToken/RefreshToken, and start rolling.&lt;/p&gt;

&lt;p&gt;Well it turned out that working with Microsoft Graph is not that easy especially if you want to schedule your application to run in the background with no human interference.&lt;/p&gt;

&lt;p&gt;While Microsoft has a good documentation, it was still vague on many critical subjects:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Authorization and what endpoint to use&lt;/li&gt;
&lt;li&gt;Authenticating using you Microsoft Account&lt;/li&gt;
&lt;li&gt;An running in the background (A Daemon app) requires high privileged admin access I don't have&lt;/li&gt;
&lt;li&gt;How to use delegate permission instead and still run your app in the background&lt;/li&gt;
&lt;li&gt;How to set up the header for resumable large files upload&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After doing what every developer does from reading documentation to looking up a solution or idea on stack overflow, I wasn't able to find a solution using python especially for the resumable large files upload.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;So I decided to publish my solution to help any fellow developer who is currently in the same shoes I was in couple weeks ago!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For the complete code please visit my &lt;a href="https://github.com/jsnm-repo/Python-OneDriveAPI-FileUpload" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  There are two major parts to this tutorial:
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Create, set up and configure the API on Azure Portal&lt;/li&gt;
&lt;li&gt;Write the python script&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Part 1 - Create, set up and configure the API on Azure Portal&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Step 1: Register your application&lt;/em&gt;&lt;br&gt;
Go to &lt;a href="https://portal.azure.com/#home" rel="noopener noreferrer"&gt;https://portal.azure.com/#home&lt;/a&gt; &lt;br&gt;
&lt;em&gt;Azure Active Directory -&amp;gt; App Registration -&amp;gt; New Registration&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Name your API&lt;/li&gt;
&lt;li&gt;Accounts in this organizational directory only (Single Tenant)&lt;/li&gt;
&lt;li&gt;Redirect URI is not needed as our app running and authenticating in the        background&lt;/li&gt;
&lt;li&gt;Click register&lt;/li&gt;
&lt;/ul&gt;
    

&lt;p&gt;Once your new API is created, click on the API and Save the following two information for the code later:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Application (client) ID &lt;/li&gt;
&lt;li&gt;Directory (tenant) ID&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fyb60xb7g5pujul4ewjiz.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fyb60xb7g5pujul4ewjiz.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then grab the OAuth 2.0 authorization endpoint (v2)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fsfs6w6ofzo608sg1na4m.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fsfs6w6ofzo608sg1na4m.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
We will use it during the authorization script to get the URL link to permissions consent&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Step 2: Configure the API permissions&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;API permissions → Add a permission → Microsoft APIs → Microsoft Graph → Delegated permissions → Select permissions Permissions needed&lt;/em&gt; “Sites.ReadWrite.All” and “Files.ReadWrite.All”&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fzrzcx3fhhrg79j0ebxju.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fzrzcx3fhhrg79j0ebxju.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Step 3: Expose the API&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;After adding the permissions in Step 3, we have to expose the API and those permissions to the scope.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fevh39if7qi1x33ywxrz8.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fevh39if7qi1x33ywxrz8.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then we need to add the client ID and select the authorized scopes we just added.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Expose an API → Add a client application → Enter Client ID → select the Authorized scopes → click add application&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fixs1rap9aud4sk99q6fp.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fixs1rap9aud4sk99q6fp.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Step 4: Edit the manifest&lt;/em&gt; &lt;strong&gt;(Very important to allow Implicit grant)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is a very important step in the API set. Go to Manifest and set the Oauth2IdToken and ImplicitFlow to true&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fruozq0pj89qkv28ouvh6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fruozq0pj89qkv28ouvh6.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now that our API is all set and configured we can start writing some Python code!!!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Part 2- Write the python script&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Our code is 2 different python scripts:&lt;/p&gt;

&lt;p&gt;1- &lt;em&gt;generateOneDriveAPIConsentURL-public.py&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Script to generate the consent URL.This script is basically run once after setting the permissions in the API setup to give the user's consent to these permissions.&lt;br&gt;
This is the best solution to run the main app in the background without using the app permissions (which pose high security risk as it is global high privileged and requires admin approval)&lt;br&gt;
Sticking to delegate permission limit the permissions to the current user privileges and in virtually all cases, it doesn't require admin approval.&lt;/p&gt;

&lt;pre&gt;
import requests
import json
from requests_oauthlib import OAuth2Session
from oauthlib.oauth2 import MobileApplicationClient

client_id = "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx"
scopes = ['Sites.ReadWrite.All','Files.ReadWrite.All']
auth_url = 'https://login.microsoftonline.com/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/oauth2/v2.0/authorize'

#OAuth2Session is an extension to requests.Session
#used to create an authorization url using the requests.Session interface
#MobileApplicationClient is used to get the Implicit Grant

oauth = OAuth2Session(client=MobileApplicationClient(client_id=client_id), scope=scopes)
authorization_url, state = oauth.authorization_url(auth_url)
consent_link = oauth.get(authorization_url)
print(consent_link.url)&lt;/pre&gt;
This script basically connect the Microsoft authorization V2. endpoint, send the Client ID and the scope of permission we are asking for then a URL will be generated and sent back in the terminal
&lt;pre&gt;c:/Users/jsnmtr/Code/onedrive/generateOneDriveAPIConsentURL-public.py
https://login.microsoftonline.com/xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx/oauth2/v2.0/authorize?response_type=token&amp;amp;client_id=xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx&amp;amp;scope=Sites.ReadWrite.All+Files.ReadWrite.All&amp;amp;state=xxxxxxxxxxxxxxxxxxxxxxxx
&lt;/pre&gt;

&lt;p&gt;Open the link in your web browser and click to accept to accept the permissions requested&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fwpt45hke2umpetcdav34.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fwpt45hke2umpetcdav34.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;2- &lt;em&gt;AutomatedOneDriveAPIUploadFiles-public.py&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This is the main script, it creates a public client application using the MSAL library, request a token on behalf of the user, gain access to Microsoft Graph and use the OneDrive API to upload files.&lt;/p&gt;

&lt;p&gt;Basic flow:&lt;br&gt;
-Importing libraries &lt;/p&gt;

&lt;pre&gt;
import os
import requests
import json
import msal
&lt;/pre&gt;

&lt;p&gt;-Configuration&lt;/p&gt;

&lt;pre&gt;
CLIENT_ID = 'xxxxxxxx-xxxxxxx-xxxxxx-xxxxxxx-xxxxxxxxxx'
TENANT_ID = 'xxxxxxxx-xxxxxxx-xxxxxx-xxxxxxx-xxxxxxxxxx'
AUTHORITY_URL = 'https://login.microsoftonline.com/{}'.format(TENANT_ID)
RESOURCE_URL = 'https://graph.microsoft.com/'
API_VERSION = 'v1.0'
USERNAME = 'xxxxxxxxx@xxxxxx.xxx' #Office365 user's account username
PASSWORD = 'xxxxxxxxxxxxxxx'
SCOPES = ['Sites.ReadWrite.All','Files.ReadWrite.All'] # Add other scopes/permissions as needed.
&lt;/pre&gt;

&lt;p&gt;-Create a public client application using the Microsoft Authentication Library (MSAL)&lt;/p&gt;

&lt;pre&gt;
#Creating a public client app, Aquire a access token for the user and set the header for API calls
cognos_to_onedrive = msal.PublicClientApplication(CLIENT_ID, authority=AUTHORITY_URL)
&lt;/pre&gt;

&lt;p&gt;-Acquire a token from Microsoft identity platform endpoint to access Microsoft Graph API&lt;/p&gt;

&lt;pre&gt;
token = cognos_to_onedrive.acquire_token_by_username_password(USERNAME,PASSWORD,SCOPES)
&lt;/pre&gt;

&lt;p&gt;-Set the request header with access token&lt;/p&gt;

&lt;pre&gt;
headers = {'Authorization': 'Bearer {}'.format(token['access_token'])}
&lt;/pre&gt;

&lt;p&gt;-Read all the file in source directory&lt;br&gt;
so we loop into the directory, get the file path, file size and read the data in the file&lt;/p&gt;

&lt;pre&gt;

#Looping through the files inside the source directory
for root, dirs, files in os.walk(cognos_reports_source):
    for file_name in files:
        file_path = os.path.join(root,file_name)
        file_size = os.stat(file_path).st_size
        file_data = open(file_path, 'rb')
&lt;/pre&gt;

&lt;p&gt;-If the file is less than 4mb:&lt;/p&gt;

&lt;pre&gt;
if file_size &amp;lt; 4100000:   
&lt;/pre&gt;

&lt;p&gt;-perform a simple upload&lt;/p&gt;

&lt;pre&gt;
#Perform simple upload to the OneDrive API
            r = requests.put(onedrive_destination+"/"+file_name+":/content", data=file_data, headers=headers)
&lt;/pre&gt;

&lt;p&gt;-If the file is larger than 4mb:&lt;br&gt;
Create an upload session&lt;/p&gt;

&lt;pre&gt;
upload_session = requests.post(onedrive_destination+"/"+file_name+":/createUploadSession", headers=headers).json()
&lt;/pre&gt;

&lt;p&gt;-Divide the file into byte chunks&lt;/p&gt;

&lt;pre&gt;
total_file_size = os.path.getsize(file_path)
chunk_size = 327680
chunk_number = total_file_size//chunk_size
chunk_leftover = total_file_size - chunk_size * chunk_number
chunk_data = f.read(chunk_size)
start_index = i*chunk_size
end_index = start_index + chunk_size
&lt;/pre&gt;

&lt;p&gt;-Set the header to match the starting index and end index of the byte chunk&lt;/p&gt;

&lt;pre&gt;
headers = {'Content-Length':'{}'.format(chunk_size),'Content-Range':'bytes {}-{}/{}'.format(start_index, end_index-1, total_file_size)}
&lt;/pre&gt;

&lt;p&gt;-Upload chunks&lt;/p&gt;

&lt;pre&gt;
chunk_data_upload = requests.put(upload_session['uploadUrl'], data=chunk_data, headers=headers)
&lt;/pre&gt;

&lt;p&gt;For the complete code please visit my &lt;a href="https://github.com/jsnm-repo/Python-OneDriveAPI-FileUpload" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To me, anytime you achieve a goal, is a success story and by  writing this code I achieved my goal to automate my reports upload process to OneDrive! SUCCESS!&lt;/p&gt;




&lt;h2&gt;
  
  
  Let me know your thoughts!
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Find me on &lt;a href="https://www.linkedin.com/in/jasonmatar/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; | &lt;a href="https://twitter.com/Jsnmtr" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; | &lt;a href="https://dev.to/jsnmtr"&gt;Dev.to&lt;/a&gt; | &lt;a href="https://stackoverflow.com/users/8206322/jason-m" rel="noopener noreferrer"&gt;StackOverflow&lt;/a&gt;
&lt;/h2&gt;

</description>
      <category>tutorial</category>
      <category>python</category>
      <category>microsoftgraph</category>
      <category>onedriveapi</category>
    </item>
  </channel>
</rss>
