使用 bash (curl) 使用 Microsoft Graph 下载文件
Downloading files with Microsoft Graph using bash (curl)
我尝试通过 Microsoft Graph 从 OneDrive 下载几个文件。
我非常接近实现我的目标。目前我已经管理了令牌系统(特别是通过 Jay Lee detailed answer), and achieved to resolve the confusion that I made among the different endpoints that exist in Microsoft Graph (thank you )。
现在我真的在处理允许下载我想要的文件的 API 的调用。我是这样进行的:
1- 正如我在 Microsoft Graph Documentation 中看到的,正常的语法如下:
curl -w %{time_total} https://graph.microsoft.com/v1.0/me/drive/items/01M...WU/content -H "Authorization: Bearer $access_token"
2- 但是,这给了我一个 302 请求,我无法在 bash 中处理。所以我寻找另一个解决方案,我发现这个 Microsoft article 解释说:
“要在 JavaScript 应用程序 中从 OneDrive 下载文件,您不能使用 /content API,因为它会响应 302 重定向 。A当需要 CORS 预检时,明确禁止 302 重定向,例如在提供授权 header.
时
相反,您的应用需要 select @microsoft.graph.downloadUrl 属性,returns 与 /content 相同 URL会重定向到 。然后可以使用 XMLHttpRequest 直接请求此 URL。因为这些 URL 是 pre-authenticated,所以无需 CORS 预检请求即可检索它们。"
它讲的是 Javascript 但我认为它可以适用于我的情况。
3- 所以我尝试了这个方法并写道:
curl "https://graph.microsoft.com/v1.0/me/drive/items/01MB...WU?select=id,@microsoft.graph.downloadUrl" -H "Authorization: Bearer $access_token"
它给了我通常允许我最终下载文件的 URL,但是当我执行它时,我得到以下响应:
代码:
curl "https://graph.microsoft.com/v1.0/$metadata#users('e967dd4d-680e-4a06-9bf7-468875e1a04c')/drive/items/$entity" -H "Authorization: Bearer $access_token"
(这正是我之前请求得到的结果)
响应:
有什么帮助吗?为什么这是一个错误的请求,因为我已经准确地输入了从 graph.microsoft.com 请求中得到的内容?
https://graph.microsoft.com/v1.0/$metadata#users('e967dd4d-680e-4a06-9bf7-468875e1a04c')/drive/items/$entity
是 @odata.context
注释,这不是您想要的。您需要将 URL 从 @microsoft.graph.downloadUrl
注释中拉出并使用它来获取文件内容。您要查找的 URL 应该在其中提及 download.aspx
。
我尝试通过 Microsoft Graph 从 OneDrive 下载几个文件。
我非常接近实现我的目标。目前我已经管理了令牌系统(特别是通过 Jay Lee detailed answer), and achieved to resolve the confusion that I made among the different endpoints that exist in Microsoft Graph (thank you
现在我真的在处理允许下载我想要的文件的 API 的调用。我是这样进行的:
1- 正如我在 Microsoft Graph Documentation 中看到的,正常的语法如下:
curl -w %{time_total} https://graph.microsoft.com/v1.0/me/drive/items/01M...WU/content -H "Authorization: Bearer $access_token"
2- 但是,这给了我一个 302 请求,我无法在 bash 中处理。所以我寻找另一个解决方案,我发现这个 Microsoft article 解释说:
“要在 JavaScript 应用程序 中从 OneDrive 下载文件,您不能使用 /content API,因为它会响应 302 重定向 。A当需要 CORS 预检时,明确禁止 302 重定向,例如在提供授权 header.
时相反,您的应用需要 select @microsoft.graph.downloadUrl 属性,returns 与 /content 相同 URL会重定向到 。然后可以使用 XMLHttpRequest 直接请求此 URL。因为这些 URL 是 pre-authenticated,所以无需 CORS 预检请求即可检索它们。"
它讲的是 Javascript 但我认为它可以适用于我的情况。
3- 所以我尝试了这个方法并写道:
curl "https://graph.microsoft.com/v1.0/me/drive/items/01MB...WU?select=id,@microsoft.graph.downloadUrl" -H "Authorization: Bearer $access_token"
它给了我通常允许我最终下载文件的 URL,但是当我执行它时,我得到以下响应:
代码:
curl "https://graph.microsoft.com/v1.0/$metadata#users('e967dd4d-680e-4a06-9bf7-468875e1a04c')/drive/items/$entity" -H "Authorization: Bearer $access_token"
(这正是我之前请求得到的结果)
响应:
有什么帮助吗?为什么这是一个错误的请求,因为我已经准确地输入了从 graph.microsoft.com 请求中得到的内容?
https://graph.microsoft.com/v1.0/$metadata#users('e967dd4d-680e-4a06-9bf7-468875e1a04c')/drive/items/$entity
是 @odata.context
注释,这不是您想要的。您需要将 URL 从 @microsoft.graph.downloadUrl
注释中拉出并使用它来获取文件内容。您要查找的 URL 应该在其中提及 download.aspx
。