- Login to Azure Portal
- Goto App Register
- Click "New registration"
- Provide name, select "Accounts in any organizational directory (Any Azure AD directory - Multitenant)"
- Now go to app overview section
- clientId <- Copy Application (client) ID
- Go to Certificates & secrets and click on "New client secret"
- Add description and set expire time
- clientSecret <- Copy Value of new client secret. Don't use Secret ID
- tenantId <- Copy Directory (tenant) ID
- Goto API Permissions. Click "Add a permission".
- Microsoft Graph > Delegated Permissions > Search "Files" and "User"
- Add below scopes a. Files.ReadWrite b. Files.ReadWrite.All c. User.Read d. User.ReadWrite
- Goto Authentication
- Advanced Settings > Allow public client flows > Set "Yes". Then save it
Last active
May 5, 2023 05:51
-
-
Save arifsuhan/edd941204c69806608b343c4ab2a6eb3 to your computer and use it in GitHub Desktop.
Upload files to onedrive
Search Error Code. Ex:
- 50158
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[azure] | |
clientId = [] | |
clientSecret = [] | |
tenantId = [] | |
authTenant = common | |
graphUserScopes = Files.ReadWrite Files.ReadWrite.All User.Read User.ReadWrite |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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