无法使用 Microsoft.Graph 通过共享 link 下载文件夹的内容

Unable to download the content of the folder via shared link using Microsoft.Graph

我正在尝试使用 Microsoft.Graph API 获取 Onedrive 中共享文件夹中包含的文件。我的代码是:

public static GraphServiceClient GetGraphServiceClient()
        {
            if (graphClient == null)
            {
                // Create Microsoft Graph client.
                try
                {
                    graphClient = new GraphServiceClient(
                        "https://graph.microsoft.com/v1.0",
                        new DelegateAuthenticationProvider(
                            async (requestMessage) =>
                            {
                                var token = await GetTokenForUserAsync();
                                requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", token);
                            }));
                    return graphClient;
                }

                catch (Exception ex)
                {
                }
            }
    void GetFile()
    {
    graphclient = AuthenticationHelper.GetGraphServiceClient();
    var request = graphclient.Shares[get_encoded_link()].Root.Children;
                var foundFile = await request.Request().GetAsync();

                var request1 = graphclient.Shares[get_encoded_link()].Items[foundFile[0].Id].Content;
                var content = await request1.Request().GetAsync();
    }

在找到的文件中,我获得了文件夹中文件的所有详细信息。但是当我使用它的 id 请求文件的内容时,我得到了这个错误 "Unexpected exception returned from the service" 我也尝试使用 root.itemsPath[filename]

请求内容
var request1 = graphclient.Shares[get_encoded_link()].Root.ItemsPath[filename];

共享 link 由范围类型为 "anonymous" 且 Link 类型为 "edit" 的其他用户创建,登录我的应用程序的用户是 try访问共享文件夹的内容。 但是得到同样的错误。 我在这里找不到我做错了什么!

感谢您的反馈。当从具有 "anonymous" 范围的其他用户创建的共享 link 下载内容时,Microsoft Graph API 似乎存在问题。

要查看这里发生了什么,您可以利用 and also the source code of Microsoft Graph .NET Client Library. You can download the source code form Releases 并将它们添加到您的项目中进行调试。

当你调用var content = await request1.Request().GetAsync();时,它最终会调用SendAsync方法来发送如下请求:

正如 document 所说,此 returns 一个 302 Found 响应重定向到 pre-authenticated 下载 URL 文件。而下载URL是在Locationheader中的response.

图库handles redirect with HandleRedirect method and sends another request with the download URL in Location。但是,此请求 returns 得到 403 Forbidden 响应。

对于 403 Forbidden 响应,图库 throws UnexpectedExceptionResponse。这就是您出现 "Unexpected exception returned from the service." 错误的原因。

解决方法:

我们无法从 Microsoft Graph API 返回的下载 URL 中下载内容,但我们可以在使用浏览器时下载。因此,我在使用 Edge 下载时检查了 HTTP(s) 流量,发现浏览器中使用的 API 端点不同。
如您所见,它使用 api.onedrive.com 作为端点而不是 graph.microsoft.com。所以我们可以更改 RequestUri 作为解决方法。

var sharingLink = @"{Sharing Link}";

var foundFile = await graphClient.Shares[UrlToSharingToken(sharingLink)].Root.Children.Request().GetAsync();

var request = graphClient.Shares[UrlToSharingToken(sharingLink)].Items[foundFile[0].Id].Content.Request().GetHttpRequestMessage();

request.RequestUri = new Uri(request.RequestUri.AbsoluteUri.Replace("graph.microsoft.com", "api.onedrive.com"));

var response = await graphClient.HttpProvider.SendAsync(request);

var stream = await response.Content.ReadAsStreamAsync();