sharepoint - 404 从 Microsoft Graph 下载 OneDrive 内容

标签 sharepoint office365 onedrive microsoft-graph-api

我正在尝试通过 Microsoft API 下载 onedrive 项目的内容。但是,无论我使用何种方法,我都会收到 404 响应。这是 python/requests 中问题的重现

import requests
import json

root_url = "https://graph.microsoft.com"
base_path = "/v1.0/<tenant_id>/users/<principal_name>/drive/"

token = "ALONGTOKEN"

headers = {"Authorization": "Bearer %s" % token}

r = requests.get(root_url + base_path + "/root/children", headers=headers)
listing = json.loads(r.text)
target = listing["value"][0]
print("Target node:")
print(json.dumps(target))
print("Target node id:")
print(target["id"])

r = requests.get(root_url + base_path + "items/" + target["id"], headers=headers)
print("Target metadata:")
print(r.text)

resp = json.loads(r.text)
download_url = resp["@microsoft.graph.downloadUrl"]
print("Target download url:")
print(download_url)

r = requests.get(download_url, headers=headers)
print("Download response code:")
print(r.status_code)
print("Download response headers:")
print(r.headers)
print("Download response cookies:")
print(r.cookies)
print("Download response redirect history:")
print(r.history)

输出如下:

Target node:
{"parentReference": {"driveId": "drive_id", "path": "/drive/root:", "id": "parent_id"}, "cTag": "\"c:{tag},1\"", "lastModifiedDateTime": "2016-08-24T17:32:45Z", "name": "birds.png", "createdDateTime": "2016-08-24T17:32:45Z", "image": {}, "webUrl": "https://org-my.sharepoint.com/personal/principal_name/Documents/birds.png", "lastModifiedBy": {"user": {"displayName": "User Name", "id": "user_id"}}, "eTag": "\"{etag},1\"", "createdBy": {"user": {"displayName": "User Name", "id": "user_id"}}, "@microsoft.graph.downloadUrl": "https://org-my.sharepoint.com/personal/principal_name/_layouts/15/download.aspx?guestaccesstoken=access_token&docid=did&expiration=2016-09-01T17%3a12%3a14.000Z&userid=uid&authurl=True&NeverAuth=True", "file": {"hashes": {}}, "id": "01L4SXJGJ2LR2PGPKJMVGZPHIADCAYJEFE", "size": 34038}

Target node id:
01L4SXJGJ2LR2PGPKJMVGZPHIADCAYJEFE

Target metadata:
{"@odata.context":"https://graph.microsoft.com/v1.0/$metadata#users('principal_name')/drive/items/$entity","@microsoft.graph.downloadUrl":"https://org-my.sharepoint.com/personal/principal_name/_layouts/15/download.aspx?guestaccesstoken=accesstoken&docid=docid&expiration=2016-09-01T17%3a12%3a15.000Z&userid=uid&authurl=True&NeverAuth=True","createdBy":{"user":{"id":"user_id","displayName":"User Name"}},"createdDateTime":"2016-08-24T17:32:45Z","eTag":"\"{etag},1\"","id":"01L4SXJGJ2LR2PGPKJMVGZPHIADCAYJEFE","lastModifiedBy":{"user":{"id":"user_id","displayName":"User Name"}},"lastModifiedDateTime":"2016-08-24T17:32:45Z","name":"birds.png","webUrl":"https://org-my.sharepoint.com/personal/principal_name/Documents/birds.png","cTag":"\"c:{ctag},1\"","file":{"hashes":{}},"image":{},"parentReference":{"driveId":"drive_id","id":"parent_id","path":"/drive/root:"},"size":34038}

Target download url:
https://org-my.sharepoint.com/personal/principal_name/_layouts/15/download.aspx?guestaccesstoken=accesstoken&docid=docid&expiration=2016-09-01T17%3a12%3a15.000Z&userid=uid&authurl=True&NeverAuth=True

Download response code:
404

Download response headers:
{'Content-Length': '13702', 'SPIisLatency': '4', 'X-Content-Type-Options': 'nosniff', 'X-AspNet-Version': '4.0.30319', 'request-id': '288b9f9d-c04a-2000-133b-ebab2f6f332b', 'Strict-Transport-Security': 'max-age=31536000', 'MicrosoftSharePointTeamServices': '16.0.0.5625', 'X-Powered-By': 'ASP.NET', 'SPRequestGuid': '288b9f9d-c04a-2000-133b-ebab2f6f332b', 'Server': 'Microsoft-IIS/8.5', 'X-MS-InvokeApp': '1; RequireReadOnly', 'X-SharePointHealthScore': '0', 'SPRequestDuration': '297', 'SharePointError': '0', 'Cache-Control': 'private', 'Date': 'Thu, 01 Sep 2016 16:12:14 GMT', 'P3P': 'CP="ALL IND DSP COR ADM CONo CUR CUSo IVAo IVDo PSA PSD TAI TELo OUR SAMo CNT COM INT NAV ONL PHY PRE PUR UNI"', 'Content-Type': 'text/html; charset=utf-8'}

Download response cookies:
<RequestsCookieJar[]>

Download response redirect history:
[]

也就是说,紧接着链接会导致 404,尽管它应该下载文件字节。我已经在 java、python、bash/curl 和浏览器中复制了它。任何人都可以帮助指出我做错了什么,或者这是 Microsoft Graph API 的问题吗?

编辑:

我还可以使用描述的/drive/items/{item-id}/content 端点重现相同的 404 here .对此端点的请求会导致 302 重定向(如文档中所述),当遵循该重定向时,会导致与上述相同的 404 行为。

编辑2:

以下是我可以在响应 header 中找到的所有请求 ID,它们看起来对 Microsoft 端的调试很有用。

对于项目对象的 200 个请求:'request-id': 'adfa3492-4825-439d-8e59-022f32e78244', 'client-request-id': 'adfa3492-4825-439d-8e59-022f32e78244'

对于下载 url 上的 404 请求:'request-id': '33e09e9d-b0c2-2000-133c-304585c15000', 'SPRequestGuid': '33e09e9d-b0c2-2000-133c-304585c15000',

此外,从 404 返回的实际 HTML 包括关联 ID:a8e09e9d-a0bb-2000-133b-ef6fc8ac7015

最佳答案

文件下载目前仅支持委派权限(例如 File.Read 范围),如记录here .您的请求是使用 应用程序 权限 Files.Read.All 和 Files.ReadWrite.All 发出的,我们正在逐步添加对它们的支持,但它们尚未完全发挥作用,因此未列出 here .

关于sharepoint - 404 从 Microsoft Graph 下载 OneDrive 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39150869/

相关文章:

c# - ITransformableFilterValues 实现,用于过滤包含 ParameterValues 的条目

javascript - 动态共享点面包屑路径编辑

c# - runspace New-MsolLicenseOptions 命令参数

c# - Azure Web API 仅适用于 Office 365 用户?

html - 从 Sharepoint 服务器更改客户端分类选择器的样式

sharepoint - 如何获取 SharePoint 列表字段值

javascript - 使用 office.js 在 Excel 中获取单个单元格格式?

c# - .NET OneDrive Api 创建文件夹问题

html - 如何在我这边播放嵌入的 OneDrive 视频?

c# - 使用 Onedrive for Business 时的文件路径