You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用Microsoft Graph获取OneDrive共享文件夹遇400错误求助

OneDrive共享文件夹API调用400错误排查

问题描述

需要列出用户OneDrive中的共享文件夹,调用Graph API的SharedWithMe方法时抛出Invalid request异常,返回400响应。几个月前使用旧版SDK无此问题,当前为.NET 8 Android应用。

调用代码

var sharedWithMeResponse = await client.Drives[driveId].SharedWithMe.GetAsSharedWithMeGetResponseAsync();

权限范围

{
    "https://graph.microsoft.com/Files.ReadWrite",
    "https://graph.microsoft.com/Files.ReadWrite.All"
}

认证流程代码

var _publicClientApp = PublicClientApplicationBuilder.Create(ClientID)
    .WithRedirectUri($"msal{ClientID}://auth")
    .Build();
...

var scopes = new List<string>
                    {
                        "https://graph.microsoft.com/Files.ReadWrite",
                        "https://graph.microsoft.com/Files.ReadWrite.All"
                    };

AuthenticationResult authenticationResult = await _publicClientApp.AcquireTokenInteractive(scopes)
    .WithParentActivityOrWindow((Activity)activity)
    .ExecuteAsync();
var authenticationProvider = new BaseBearerTokenAuthenticationProvider(new TokenFromAuthResultProvider() { AuthenticationResult = authenticationResult });
var client = new GraphServiceClient(new HttpClient(), authenticationProvider);

// 失败的调用
var sharedWithMeResponse = await client.Drives[driveId].SharedWithMe.GetAsSharedWithMeGetResponseAsync();

自定义认证提供者类

public class TokenFromAuthResultProvider : IAccessTokenProvider
{
    public AuthenticationResult AuthenticationResult
    {
        get;
        set;
    }
    public async Task<string> GetAuthorizationTokenAsync(Uri uri, Dictionary<string, object>? additionalAuthenticationContext = null,
        CancellationToken cancellationToken = new CancellationToken())
    {
        return AuthenticationResult.AccessToken;
    }

    public AllowedHostsValidator AllowedHostsValidator { get; }
}

异常截图

异常截图1
异常截图2
异常截图3


问题排查及修复方案

1. 修复自定义认证提供者的AllowedHostsValidator

TokenFromAuthResultProvider类中AllowedHostsValidator未初始化,导致Graph SDK无法验证请求主机合法性,引发400错误。修改代码如下:

public class TokenFromAuthResultProvider : IAccessTokenProvider
{
    public AuthenticationResult AuthenticationResult
    {
        get;
        set;
    }
    
    public async Task<string> GetAuthorizationTokenAsync(Uri uri, Dictionary<string, object>? additionalAuthenticationContext = null,
        CancellationToken cancellationToken = new CancellationToken())
    {
        return AuthenticationResult.AccessToken;
    }

    // 初始化AllowedHostsValidator
    public AllowedHostsValidator AllowedHostsValidator { get; } = new AllowedHostsValidator();
}

2. 优化SharedWithMe调用路径

若访问当前用户默认OneDrive,直接使用client.Me.Drive.SharedWithMe替代指定driveId的方式,避免因driveId错误导致请求无效:

var sharedWithMeResponse = await client.Me.Drive.SharedWithMe.GetAsSharedWithMeGetResponseAsync();

3. 验证SDK版本与权限

  • 确认使用的Microsoft Graph SDK版本与官方文档匹配,新版SDK可能调整了API调用逻辑;
  • 检查authenticationResult.Scopes,确保用户实际授权了Files.ReadWrite和Files.ReadWrite.All权限。

内容的提问来源于stack exchange,提问作者Philipp

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 19:50:56