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

时间:2019-08-27 14:24:43

标签: c# microsoft-graph microsoft-graph-sdks

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

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;
}

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

Code: ResourceNotFound
Message: Invalid version: me

Inner error

我已将https://jwt.ms/中的令牌签到并验证了"aud": "https://graph.microsoft.com"

2 个答案:

答案 0 :(得分:3)

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

https://graph.microsoft.com/currentServiceVersion

所以这行

var graphClient = new GraphServiceClient("https://graph.microsoft.com", new DelegateAuthenticationProvider(async (requestMessage) =

应该是

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

或/ beta(如果要使用)

答案 1 :(得分:1)

问题不是您的令牌而是您的URL,它缺少版本号。

右键查询:

Image 1

您的查询:

Image 2