图返回“代码:ResourceNotFound 消息:无效版本:我内部错误”

oha*_*nho 4 c# microsoft-graph-sdks microsoft-graph-api

我正在尝试执行读取用户配置文件的简单操作。在我授予此操作的相关权限后,我可以通过编写以下代码来获取令牌:

static void Main(string[] args)
{
    var getToken = new GetTokenEntity()
    {
        Authority = "https://login.microsoftonline.com/common",
        Resource = "https://graph.microsoft.com",
        UserName = "myusername",
        ClientId = "appclientidguid",
        Password = "somepass"
    };

    var graphClient = new GraphServiceClient("https://graph.microsoft.com", new DelegateAuthenticationProvider(async(requestMessage) =>
    {
        var authResult = await GetToken(getToken);
        requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", authResult.AccessToken);
    }));

    var inbox = GetMessages(graphClient).GetAwaiter().GetResult();
}

public async static Task<AuthenticationResult> GetToken(GetTokenEntity getToken)
{
    var authenticationContext = new AuthenticationContext(getToken.Authority);
    var authenticationResult = await authenticationContext
        .AcquireTokenAsync(getToken.Resource, getToken.ClientId,
            new UserPasswordCredential(getToken.UserName, getToken.Password));
    return authenticationResult;
}

public async static Task<User> GetMessages(GraphServiceClient graphClient)
{
    var currentUser = await graphClient.Me.Request().GetAsync();
    return currentUser;
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,在收到令牌后,这一行:await graphClient.Me.Request().GetAsync();失败并出现此异常:

Code: ResourceNotFound
Message: Invalid version: me

Inner error
Run Code Online (Sandbox Code Playgroud)

我已经在https://jwt.ms/ 中检查了我的令牌并验证了"aud": "https://graph.microsoft.com".

Gle*_*les 5

根据文档https://github.com/microsoftgraph/msgraph-sdk-dotnet/blob/dev/docs/overview.md(以及 Visual Studio 中的智能提示),您的基本 URL 应该是

https://graph.microsoft.com/currentServiceVersion
Run Code Online (Sandbox Code Playgroud)

所以这一行

var graphClient = new GraphServiceClient("https://graph.microsoft.com", new DelegateAuthenticationProvider(async (requestMessage) =
Run Code Online (Sandbox Code Playgroud)

应该是

var graphClient = new GraphServiceClient("https://graph.microsoft.com/v1.0", new DelegateAuthenticationProvider(async (requestMessage) =
Run Code Online (Sandbox Code Playgroud)

或 /beta 如果你想使用它