python
2 TopicsUpload files to onedrive personal using python on non-interactive method
As part of my python project, I need to upload a file to onedrive personal folder non-interactive way. I have created an app registration in Azure provided below API permissions: Account Type: Accounts in any organizational directory (Any Microsoft Entra ID tenant - Multitenant) and personal Microsoft accounts (e.g. Skype, Xbox) API Permissions: Python code: import requests # Replace with your details client_id = 'xxxxxx' client_secret = 'xxxxx' tenant_id = 'xxxxx' filename = 'C:/ABB_2025-01-13.csv' onedrive_folder = 'CloudOnly/test' user_id = 'c19d6ba9-e7d1-xxxxx-xxxxxx' # Get the access token url = f'https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token' data = { 'grant_type': 'client_credentials', 'client_id': client_id, 'client_secret': client_secret, 'scope': 'https://graph.microsoft.com/.default' } response = requests.post(url, data=data) token = response.json().get('access_token') # Upload the file to OneDrive headers = { 'Authorization': f'Bearer {token}', 'Content-Type': 'application/octet-stream' } file_content = open(filename, 'rb').read() upload_url = f'https://graph.microsoft.com/v1.0/users/{user_id}/drive/root:/{onedrive_folder}/{filename.split("/")[-1]}:/content' upload_response = requests.put(upload_url, headers=headers, data=file_content) if upload_response.status_code == 201: print('File uploaded successfully!') else: print('Error uploading file:', upload_response.json()) Error: Error uploading file: {'error': {'code': 'BadRequest', 'message': 'Tenant does not have a SPO license.', 'innerError': {'date': '2025-01-14T02:15:47', 'request-id': 'cf70193e-1723-44db-9f5e-xxxxxxx', 'client-request-id': 'cf70193e-1723-44db-9f5e-xxxxxxx'}}} How to resolve this ?62Views0likes0CommentsListing Files form OneDrive Using Python
Hello, I am trying to build an App in Python that automate files creation and deletion in OneDrive. I decided to use Microsoft Graph API for this. I wrote the following script to get started. import msal import requests import json # Define the MSAL application configuration client_id = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' authority = 'https://login.microsoftonline.com/common' scope = ['https://graph.microsoft.com/.default'] # Create a PublicClientApplication object app = msal.PublicClientApplication(client_id=client_id, authority=authority) # Check if the user has already signed in accounts = app.get_accounts() if accounts: # Get an access token silently result = app.acquire_token_silent(scope, account=accounts[0]) else: # Sign in and get an access token interactively result = app.acquire_token_interactive(scope) # Get the access token from the result access_token = result['access_token'] headers = {'Authorization': 'Bearer ' + access_token, 'Content-Type': 'application/json'} # url = "https://graph.microsoft.com/v1.0/me" # works just fine url = "https://graph.microsoft.com/v1.0/me/drive/root/children" # send a request response = requests.get(url,headers=headers) # check the status code of the response if response.status_code == requests.codes.ok: print(response.json()) else: # Print the error message if the request failed print(f'Request failed with status code: {response.status_code}') print(response.json()) Fetching the profile information using the URL in line 27 works as expected. However, listing the drive items using the URL in Line 28 gives the following error: {'error': {'code': 'itemNotFound', 'message': 'Item not found', 'innerError': {'date': '2023-04-07T11:58:04', 'request-id': '.........', 'client-request-id': '.........'}}} This is the App permission: Any idea what is going on?6.1KViews0likes1Comment