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"
.
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
Problem is not with your token but you URL, it is missing a version number.
Right Query:
Your Query:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With