如何使用Python从单驱动器文档文件夹中读取.txt文件?

2024-03-28 21:04:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经注册了该应用程序,添加了读写权限,获得了管理员同意,并获得了令牌。当我发出GET请求时,我得到一个错误

这是我的Python代码:

import requests, sys, os, json

# Get token
params = {
    'client_id': clientID,
    'scope': 'https://graph.microsoft.com/.default', 
    'client_secret': clientSecret,
    'grant_type': 'client_credentials'
 }

response = requests.post('https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token', data=params)
jresp = response.json()
access_token = jresp['access_token']

# Get item
header = {'Authorization': 'Bearer ' + access_token}
response = requests.get('https://graph.microsoft.com/v1.0/me/drive/root:/Documents/WildRydes.txt', headers = header)
print response
print response.text
print response.content
sys.exit()

这是返回的内容:

<Response [500]>
{
  "error": {
    "code": "BadRequest",
    "message": "Unable to retrieve user's mysite URL.",
    "innerError": {
      "request-id": ,
      "date": "2020-04-29T12:42:06"
    }
  }
}

在过去的两天里,我一直在关注MicrosoftGraphAPI文档。他们很擅长理论,但不善于展示具体的例子。或者他们擅长藏在那里


Tags: httpscomclienttokenidjsongetaccess