Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Graph returns "Code: ResourceNotFound Message: Invalid version: me Inner error"

I'm trying to perform a simple operation of reading a user profile. After I granted relevant permissions for this operation, I was able to acquire a token by writing the following code:

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

Unfortunately, after receiving a token, this line: await graphClient.Me.Request().GetAsync(); fails with this exception:

Code: ResourceNotFound
Message: Invalid version: me

Inner error

I have checked my token in https://jwt.ms/ to and verified that "aud": "https://graph.microsoft.com".

like image 204
ohadinho Avatar asked Aug 27 '19 14:08

ohadinho


2 Answers

As per the docs https://github.com/microsoftgraph/msgraph-sdk-dotnet/blob/dev/docs/overview.md (and the intellisense hint in visual studio) your base URL should be

https://graph.microsoft.com/currentServiceVersion

So this line

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

should be either

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

or /beta if you want to use that

like image 139
Glen Scales Avatar answered Oct 17 '22 12:10

Glen Scales


Problem is not with your token but you URL, it is missing a version number.

Right Query:

Image 1

Your Query:

Image 2

like image 29
Hannel Avatar answered Oct 17 '22 12:10

Hannel