Not able to download files from inside folder, OneDrive, Microsoft Graph, Python

白昼怎懂夜的黑 提交于 2020-06-09 05:24:07

问题


I am trying to download files from onedrive using microsoft graph api, I have File named "Knox EARNSTSALV2020.xlsx" inside Folder "New Folder", but I am getting error, I can download file from outside of the folder.

Error:

b'{\r\n  "error": {\r\n    "code": "itemNotFound",\r\n    "message": "Item not found",\r\n    "innerError": {\r\n      "request-id": "8c4f973a-cd22-48eb-bdfd-f5eb8a051389",\r\n      "date": "2020-05-09T10:55:40"\r\n    }\r\n  }\r\n}'

Code Reference: Download files from personal OneDrive using Python

import sys, os, time, requests
import pandas as pd
import urllib.parse

OneDrive_FilePath = 'New Folder/Knox EARNSTSALV2020.xlsx'

OneDrive_FileURL = 'https://graph.microsoft.com/v1.0/me/drive/root:/' + OneDrive_FilePath + ':/content'
OneDrive_FileURL = urllib.parse.quote(OneDrive_FileURL, safe=':/')
print(OneDrive_FileURL)

Client_Id = 'XXXX'
Tenant_Id = 'YYYYY'
Refresh_Token_First = 'ZZZZZ'

PostStr = {'grant_type': 'refresh_token', 'client_id': Client_Id, 'refresh_token': Refresh_Token_First}

Token_Response = requests.post('https://login.microsoftonline.com/' + Tenant_Id + '/oauth2/v2.0/token', data=PostStr)

Access_Token = Token_Response.json()['access_token']
New_Refresh_Token = Token_Response.json()['refresh_token']

if Access_Token is None or New_Refresh_Token is None:
    print('\n> Failed: Access_Token NOT Retrieved')
    sys.exit()

Response = requests.get(OneDrive_FileURL, headers={'Authorization': 'Bearer ' + Access_Token})

if Response.status_code == 200:
    print('\n> Response Success')

    with open('Excel File.xlsx', 'wb') as File:
    File.write(Response.content)
    print('\n> File Downloaded')
else:
    print('\n> Failed:', Response.status_code)
    print(Response.content)

回答1:


I also have spaces in my path and I don't use urllib.parse.quote. Try to call the get request with: https://graph.microsoft.com/v1.0/me/drive/root:/New Folder/Knox EARNSTSALV2020.xlsx:/content



来源:https://stackoverflow.com/questions/61695577/not-able-to-download-files-from-inside-folder-onedrive-microsoft-graph-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!