Upload file to onedrive personal using python in non-interactive way

Anoop Challiyil Ratnan 0 Reputation points
2025-01-14T09:11:23.5333333+00:00

I have a requirement to upload csv files into onedrive personal using python in non-ineractive way. I have created an app registration in my Azure tenant with below API permissions

Type:
Accounts in any organizational directory (Any Microsoft Entra ID tenant - Multitenant) and personal Microsoft accounts (e.g. Skype, Xbox)"

API Permissions:
Screenshot 2025-01-14 075129

My Code:


import requests

# Replace with your details
client_id = 'xxxxx'
client_secret = 'xxxx'
tenant_id = '052c8b5b-xxxx'
filename = 'C:/test.csv'
onedrive_folder = 'CloudOnly/test'
user_id = 'xxxx-e7d1-44dc-a846-5exxxx'

# 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 message:
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-xxxxx', 'client-request-id': 'cf70193e-1723-44db-9f5e-xxxx'}}}

How to solve this ? Please note, I need to use a non-interactive way as this has to be setup on an ubuntu server to run the script on daily basis and upload the CSV files to personal onedrive.

Microsoft 365 and Office Development Other
Microsoft 365 and Office OneDrive For business Windows
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Jiajing Hua 18,060 Reputation points Moderator
    2025-01-15T01:18:16.1066667+00:00

    Hi @Anoop Challiyil Ratnan

    Because your issue is related to code development, it is recommended to post the thread to OneDrive for Developer forum:

    https://techcommunity.microsoft.com/category/onedriveforbusiness/discussions/onedrivedeveloper

    The reason why we recommend posting appropriately is you will get the most qualified pool of respondents, and other partners who read the forums regularly can either share their knowledge or learn from your interaction with us.

    Thank you for your understanding.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.