0

I am using the below script to upload to sharepoint that I found here. How do I authorize a Python script to upload to SharePoint Online? . I created an app in azure correctly, I was able to get the site-id and drive-id but whenever I run the script I keep getting a

requests.exceptions.connectionError: ('Connection aborted.', ConnectionResetError(10054, 'An Existing connection was focibly closed by the remote host', None, 10054, None))
import os
import requests

# Azure AD app registration credentials
client_id = '376f340a-7b96-40c6-89fb-xxxxxxxxx'
client_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxx'
tenant_id = '3f5c7a77-062d-426c-8582-1xxxxxxxxxxx'

# SharePoint Online site URL and library name
site_id = '6d4d4ae7-be11-47d0-a9ad-xxxxxxxxxxx'
library_name = 'sridoclib'
drive_id = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

# Authenticate and get an access token
auth_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(auth_url, data=data)
access_token = response.json()['access_token']

# Upload a file to the SharePoint document library using the Microsoft Graph API
file_path = 'C:/Users/sridevi/Desktop/test.txt' #local file path
file_name = 'test.txt'
folder_name = 'sri'
upload_url = f'https://graph.microsoft.com/v1.0/sites/{site_id}/drives/{drive_id}/items/root:/{folder_name}/{file_name}:/content'
headers = {
    'Authorization': f'Bearer {access_token}',
    'Content-Type': 'application/octet-stream',
    'Content-Length': str(os.path.getsize(file_path))
}
2

1 Answer 1

1

I used the same script and I had a different problem, as I was unable to get the access token.

Perhaps it is a dumb question but the code you posted is just the example file and you have yours with the correct client_id (app_id), secret, tenant_id, etc?

Also,check the permissions for the app. I managed to get it working by adding delegated and application permissions and then granting admin privileges to those who needed it: permissions image

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.