【问题标题】:404 Downloading OneDrive content from Microsoft Graph404 从 Microsoft Graph 下载 OneDrive 内容
【发布时间】:2016-11-03 00:06:16
【问题描述】:

我正在尝试通过 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 的问题吗?

编辑:

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

EDIT2:

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

对于item对象的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

【问题讨论】:

  • 您能否提供响应标头中的“request-id”?
  • 它的 e04e9d9d-9070-2000-133b-ef376013cbdc
  • 抱歉,澄清一下:上面的 request-id 是针对 404 响应的。让我知道您是否需要对象初始获取的请求 ID。
  • 我也使用了相同的图形 api,但下载对我来说很成功!我的客户是java!你能提供更多细节吗?您是否要下载自己的文档?还是你有足够的权限?
  • @PradeepNeerukonda 我正在尝试使用委派权限下载文档以供使用。我的 AzureAD 应用程序有权访问 microsoft graph 和 sharepoint 中的所有内容。但是,downloadUrl 属性根本不需要身份验证。我的理解是,该链接在生成后的短时间内(几分钟)内可公开使用。如果您认为我应该提供更多信息,请告诉我。

标签: sharepoint office365 onedrive microsoft-graph-api


【解决方案1】:

文件下载目前仅支持 委派 权限(例如 File.Read 范围),如文档 here 所述。您的请求是使用 application 权限 Files.Read.All 和 Files.ReadWrite.All 提出的,我们正在逐步添加对它们的支持,但它们尚未完全正常运行,并且未列出 here .

【讨论】:

  • 这仍然是真的吗?
  • 这仍然是一个悬而未决的问题吗?是否有预计何时更改?
【解决方案2】:

你能检查 item_id 实际上是一个项目的 ID 吗?如果您正在处理 GET /v1.0/users//drive/items/ 返回的集合,该集合将返回一个文件夹和项目元数据数组。如果您针对文件夹尝试请求,您将得到如您所描述的 404。这是我可以重现您的问题的唯一方法。如果这不是问题,请提供请求/响应跟踪,以便我们查看错误详细信息。

【讨论】:

  • 嗨迈克尔,感谢您的回复。我确信 item_id 是文件的 id,因为初始调用返回一个包含“文件”属性的 json 对象。根据文档,这意味着该对象是一个文件。我添加了“EDIT2”,其中包含我能找到的所有请求响应跟踪信息,如果我应该包括其他内容,请告诉我。
  • @User1234567890 我无法使用 RequestIds 来查找您的电话。你能告诉我们最初的电话吗?您获得 item_id 的那个?请显示请求,以及经过处理(可能是缩写)的响应正文。
  • 嗨,迈克尔,感谢您的跟进。请再次查看我最初问题中的代码,特别是在我使用 item_id 请求项目的第一个 requests.get 调用中。对此调用的响应是 200,其中包含完整的项目描述,表明这是一个有效的项目 ID。您可以在我的问题的第二个代码块中查看此响应。我遇到的问题是该项目响应提供的 downloadUrl 解析为 404,而不是下载文件的字节。由于项目 ID 不正确,不会出现 404。
  • 嗨迈克尔,我更新了上面的脚本以反映您的要求。它现在包括通过根文件夹列出以查找目标项目的代码。希望这将有助于澄清问题。您对如何进一步调试此问题有任何建议吗?谢谢你的建议。
  • @User1234567890 我无法重现您的问题。我成功地使用密码授予流程在测试用例中获取访问令牌并访问调用帐户拥有的资源。在您的情况下,我假设调用者是一项服务,因为您提到了委派权限,并且正在访问另一个帐户中的资源。如果您使用资源所有者的访问令牌进行调用,您能否将服务排除在外并重现问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多