Skip to content

Instantly share code, notes, and snippets.

@arifsuhan
Last active May 5, 2023 05:51
Show Gist options
  • Save arifsuhan/edd941204c69806608b343c4ab2a6eb3 to your computer and use it in GitHub Desktop.
Save arifsuhan/edd941204c69806608b343c4ab2a6eb3 to your computer and use it in GitHub Desktop.
Upload files to onedrive
  1. Login to Azure Portal
  2. Goto App Register
  3. Click "New registration"
  4. Provide name, select "Accounts in any organizational directory (Any Azure AD directory - Multitenant)"
  5. Now go to app overview section
  6. clientId <- Copy Application (client) ID
  7. Go to Certificates & secrets and click on "New client secret"
  8. Add description and set expire time
  9. clientSecret <- Copy Value of new client secret. Don't use Secret ID
  10. tenantId <- Copy Directory (tenant) ID
  11. Goto API Permissions. Click "Add a permission".
  12. Microsoft Graph > Delegated Permissions > Search "Files" and "User"
  13. Add below scopes a. Files.ReadWrite b. Files.ReadWrite.All c. User.Read d. User.ReadWrite
  14. Goto Authentication
  15. Advanced Settings > Allow public client flows > Set "Yes". Then save it
[azure]
clientId = []
clientSecret = []
tenantId = []
authTenant = common
graphUserScopes = Files.ReadWrite Files.ReadWrite.All User.Read User.ReadWrite
azure-core==1.23.1
azure-identity==1.10.0
certifi==2021.10.8
cffi==1.15.0
charset-normalizer==2.0.12
cryptography==37.0.1
idna==3.3
msal==1.17.0
msal-extensions==1.0.0
msgraph-core==0.2.2
portalocker==2.4.0
pycparser==2.21
PyJWT==2.3.0
requests==2.27.1
six==1.16.0
typing_extensions==4.2.0
urllib3==1.26.9
from azure.identity import DeviceCodeCredential, ClientSecretCredential
from msgraph.core import GraphClient
import configparser
import requests
def get_user_token(azureSettings):
client_id = azureSettings.get("clientId")
tenant_id = azureSettings.get("authTenant")
graph_scopes = azureSettings.get("graphUserScopes")
device_code_credential = DeviceCodeCredential(client_id, tenant_id = tenant_id)
user_client = GraphClient(credential = device_code_credential, scopes = graph_scopes.split(' '))
access_token = device_code_credential.get_token(graph_scopes)
return access_token.token
def call_get(graph_url, access_token):
headers = {'Authorization': f'Bearer {access_token}'}
response = requests.get(graph_url, headers=headers)
return response.json()
def call_put(graph_url, access_token, file_path):
headers = {'Authorization': 'Bearer ' + access_token}
files = {'file': open(file_path, 'rb')}
response = requests.put(graph_url, headers=headers, files=files)
print(response.status_code)
config = configparser.ConfigParser()
config.read([credential_path])
azureSettings = config['azure']
token = get_user_token(azureSettings)
print(token)
call_get("https://graph.microsoft.com/v1.0/me", token)
call_get("https://graph.microsoft.com/v1.0/me/drive/root/children", token)
call_get("https://graph.microsoft.com/v1.0/me/drives", token)
call_get("https://graph.microsoft.com/v1.0/me/drive/recent", token)
call_get("https://graph.microsoft.com/v1.0/me/drive/root:/[foldername]:/children", token)
file_path = [/../../hello.pdf]
file_name = [hello.pdf]
folder_name = "myDrive"
url = "https://graph.microsoft.com/v1.0/me/drive/root:/" + folder_name +"/"+ file_name +":/content"
call_put(url,token,file_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment