question

SagarPatel-1778 avatar image
0 Votes"
SagarPatel-1778 asked saldana-msft edited

Issue with generate authProvider using Graph API

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.


azure-active-directorymicrosoft-graph-sdkmicrosoft-graph-files
error.png (126.3 KiB)
error2.png (131.6 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Adding right tags/teams to assist

0 Votes 0 ·

1 Answer

DianaWanjuhi-1579 avatar image
0 Votes"
DianaWanjuhi-1579 answered

Hello @SagarPatel-1778 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.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.