Cannot Upload File to One Drive via Python

I have tried to upload file to one drive by using python code:

Please my upload script below for reference:

url = f'https://graph.microsoft.com/v1.0/users/{user_id}/drive/root:/{local_file_path}:/createUploadSession'
headers = {
    'Authorization': 'Bearer {}'.format(access_token),
    'Content-Type': 'application/json'
}
session_data = {
    "item": {
        "@microsoft.graph.conflictBehavior": "replace",  # Action on conflict (replace, fail, etc.)
        "name": "2.txt"  # The file name in OneDrive
    }
}

upload_session = requests.post(url=url, headers=headers,json=session_data).json()

Error Message:

{"error": {"code": "Request_BadRequest", "message": "Specified HTTP method is not allowed for the request target.",
"innerError": {"date": "2024-11-07T04:21:05", "request-id": "7be5355c-381a-4ea8-9d89-0b0a99208bb4", "client-request-id":
"7be5355c-381a-4ea8-9d89-0b0a99208bb4"}}}
Вернуться на верх