Код: Выделить всё
import msal
import requests
# Azure AD app credentials
client_id = 'xxx9846xxxx'
client_secret = 'xxxTdxxx'
tenant_id = 'xxx-xxxx'
# Authority URL for your tenant
authority = f'https://login.microsoftonline.com/{tenant_id}'
# Scopes needed for OneDrive file operations
scopes = ['https://graph.microsoft.com/.default']
# Initialize the MSAL ConfidentialClientApplication
app = msal.ConfidentialClientApplication(
client_id,
client_credential=client_secret,
authority=authority
)
# Get the access token
token_response = app.acquire_token_for_client(scopes=scopes)
access_token = token_response.get('access_token')
if not access_token:
raise Exception("Could not acquire access token")
# Define the file to upload
file_path = 'C:/test.csv'
# Microsoft Graph API endpoint for OneDrive (using Application Permissions)
upload_url = 'https://graph.microsoft.com/v1.0/me/drive/root:/Documents/' + file_name + ':/content'
# Open the file in binary mode
with open(file_path, 'rb') as file:
file_content = file.read()
# Make the PUT request to upload the file
headers = {
'Authorization': f'Bearer {access_token}',
'Content-Type': 'application/octet-stream'
}
response = requests.put(upload_url, headers=headers, data=file_content)
# Check if the file upload was successful
if response.status_code == 201:
print(f'File uploaded successfully to OneDrive: {file_name}')
else:
print(f'Error uploading file: {response.status_code}, {response.text}')
Ошибка загрузки файла: 400,
{"ошибка": {"code":"BadRequest","message":"/me запрос действителен только
с делегированной аутентификацией
flow.","innerError":{"date":"2025-01-13T17:06:35","request-id":"5959d049-9ad7-4ce d-b6fc-00dddd242","client-request-id":"5959d049-9ad7-4ced-b6fc-0079ddddd"}}}
Как устранить эту ошибку? или любой альтернативный способ загрузки файлов
Подробнее здесь: https://stackoverflow.com/questions/793 ... active-way