Issue with generate authProvider using Graph API

Sagar Patel 1 Reputation point
2021-05-12T11:38:17.997+00:00

I am trying to generate authProvider for onedrive api using Graph API in asp.net MVC c# but i am getting the following error.

When I used AuthorizationCodeProvider then i will get bellow error:

private const string ClientId = "87b15343-d439-4e15-b6da-e0cd4c6f5acd";  
string clientSecret = "uxhNa4_4_~2eT4xM-E8BuoJr2EqGbif5.2";  
 static IEnumerable<string> scopes = new List<string> {  
                    "Files.Read",  
                    "Files.Read.All",  
                    "Files.ReadWrite",  
                    "Files.ReadWrite.All",  
                    "Mail.Read",  
                    "Mail.ReadBasic",  
                    "Mail.ReadWrite"  
            };  
  
IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder  
                .Create(ClientId)  
                .WithRedirectUri("https://localhost:44382")  
                .WithClientSecret(clientSecret) // or .WithCertificate(certificate)  
                .Build();  
  
AuthorizationCodeProvider authProvider = new AuthorizationCodeProvider(confidentialClientApplication, scopes);  
GraphServiceClient graphClient = new GraphServiceClient(authProvider);  
IDriveItemChildrenCollectionPage DriveItem = await graphClient.Me.Drive.Root.Children.Request().GetAsync();  

95964-error.png

And Then after i got some Links and changed my code according that. but still im getting error.
Link : https://stackoverflow.com/questions/62142905/using-graphserviceclient-to-retrieve-group-info-of-azure-ad-members

IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder  
                    .Create(ClientId)  
                    .WithRedirectUri("https://localhost:44382")  
                    .WithClientSecret(clientSecret) // or .WithCertificate(certificate)  
                    .Build();  
      
    ClientCredentialProvider authProvider = new ClientCredentialProvider(confidentialClientApplication);  
    GraphServiceClient graphClient = new GraphServiceClient(authProvider);  
    IDriveItemChildrenCollectionPage DriveItem = await graphClient.Me.Drive.Root.Children.Request().GetAsync();  

95974-error2.png

But i'm passing access token directly from graph-explorer then its work fine.
Can someone help in fixing this issue.

Microsoft Security Microsoft Entra Microsoft Entra ID
Microsoft Security Microsoft Graph
{count} votes

1 answer

Sort by: Most helpful
  1. Diana Wanjuhi 1,376 Reputation points
    2021-05-19T13:45:40.483+00:00

    Hello @Sagar Patel in your case, you are trying to acquire an access token on behalf of the user In the second snippet posted, your code seems to be missing scopes, you need to request the appropriate scopes for graph api token to access drive items. Also see documentation on Acquiring-tokens-with-authorization-codes-on-web-apps

    Let me know if this helps and if you have further questions,

    Diana.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.