[ACCEPTED]-Code: BadRequest Message: /me request is only valid with delegated authentication flow-onedrive

Accepted answer
Score: 12

Client credential flow will generate the 23 token on behalf the app itself, so in this 22 scenario, users don't need to sign in first 21 to generate the token stand for the user 20 and then call the api. And because of the 19 design,when you used Me in the graph SDK, your 18 code/app don't know who is Me so it can't 17 work. You should know the user_id first 16 and use /users/{id | userPrincipalName} instead of /Me, in the SDK, that is 15 graphClient.Users["your_user_id"] instead of graphClient.Me

In your scenario, there're 2 14 solutions, one way is using delegated authentication 13 flow like what you said in your title, another 12 way is get the user id before calling the 11 graph api so that you can use Users["id"] but not Me

===================== Update=========================

I 10 haven't finished the code yet but I found 9 the correct solution now.

Firstly, we can 8 upload file to one drive by this api, you may check 7 the screenshot if this is one drive or sharepoint:


enter image description here enter image description here

If 6 it is, then the next is easy, using the 5 code below to get an access token and send 4 http request to calling the api:

var scopes = new[] { "https://graph.microsoft.com/.default" };
var tenantId = "tenant_name.onmicrosoft.com";
var clientId = "your_azuread_clientid";
var clientSecret = "corresponding_client_secret";
var clientSecretCredential = new ClientSecretCredential(
    tenantId, clientId, clientSecret);
var tokenRequestContext = new TokenRequestContext(scopes);
var token = clientSecretCredential.GetTokenAsync(tokenRequestContext).Result.Token;

I know it's 3 complex because the api is not the same 2 as this one which has SDK sample, but I think it 1 also deserves to try if they are similar.

More Related questions