无法从文件夹,OneDrive,Microsoft Graph,Python内部下载文件 [英] Not able to download files from inside folder, OneDrive, Microsoft Graph, Python

查看:140
本文介绍了无法从文件夹,OneDrive,Microsoft Graph,Python内部下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Microsoft图形API从onedrive下载文件,我在新建文件夹"文件夹中有一个名为"Knox EARNSTSALV2020.xlsx"的文件,但出现错误,我可以从文件夹外部下载文件.

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.

错误:

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}'

代码参考:使用Python从个人OneDrive下载文件

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)

推荐答案

我的路径中也有空格,并且我不使用urllib.parse.quote.尝试使用以下命令调用get请求:https://graph.microsoft.com/v1.0/me/drive/root:/New Folder/Knox EARNSTSALV2020.xlsx:/content

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

这篇关于无法从文件夹,OneDrive,Microsoft Graph,Python内部下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆