ClientSecretCredential authentication failed

Mario Malvido 0 Reputation points
2024-05-06T21:32:48.83+00:00

This is my code

FolioFactory folioFactory = new FolioFactory();
ConfigOneDrive objOneDrive = folioFactory.ConsultarConfigOneDrive(e => e.IdEntity == IdEntity).FirstOrDefault();

var scopes = new[] { "https://graph.microsoft.com/.default" };

string clientId = objOneDrive.Client_ID;
string clientSecret = objOneDrive.Client_Secret;
string tenantId = objOneDrive.Tenant_ID;

// using Azure.Identity; 
var options = new TokenCredentialOptions
{
    AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
};

var clientSecretCredential = new ClientSecretCredential(tenantId, clientId, clientSecret, options);

var graphClient = new GraphServiceClient(clientSecretCredential, scopes);

var contacts = await graphClient
              .Me
              .Drive
              .Root // <-- this is the root of the drive itself
              .Children // <-- this is the DriveItem collection
              .Request()
              .GetAsync();

on

var contacts = await graphClient

I get [AuthenticationFailedException: ClientSecretCredential authentication failed: Retry failed after 4 tries. Retry settings can be adjusted in ClientOptions.Retry or by configuring a custom retry policy in ClientOptions.RetryPolicy.]

My Client_ID, Secret and Tenant ID are correct. I have al ready User.Read delegated permisions. Also Sites.Read.All, User.Read.All. I have tried everything but I can't move forward

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,785 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,353 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. CarlZhao-MSFT 37,626 Reputation points
    2024-05-07T07:56:20.3033333+00:00

    Hi @Mario Malvido

    The application context can only be used to call the /users/{user_id} endpoint and not the /me endpoint.

    Additionally, /root does not exist in graph SDK 5.0 and above, the root property on driveItem is a simply shorthand to .../items/root and is omitted to avoid generating paths that have alternatives for the purposes of optimising metadata/sdk size.

    So if you are trying to retrieve a list of files in the root of the user's drive, then change your code snippet to the following:

    using Microsoft.Graph;
    using Azure.Identity;
    
    
    var scopes = new[] { "https://graph.microsoft.com/.default" };
    
    var tenantId = "xxxxxxxxxxxxxxxxxx";
    
    var clientId = "xxxxxxxxxxxxxxxxx";
    
    var clientSecret = "xxxxxxxxxxxxxxxxxxxxxxxx";
    
    // using Azure.Identity; 
    var options = new TokenCredentialOptions
    {
        AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
    };
    
    // https://learn.microsoft.com/dotnet/api/azure.identity.clientsecretcredential
    var clientSecretCredential = new ClientSecretCredential(
        tenantId, clientId, clientSecret, options);
    
    /*var token = await clientSecretCredential.GetTokenAsync(new TokenRequestContext(scopes));
    Console.WriteLine($"Access token: {token.Token}");*/
    
    var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
    
    var rootFolders = await graphClient.Drives["{drive_id}"].Items["root"].Children.GetAsync();
    
    Console.WriteLine(rootFolders);
    

    User's image

    Hope this helps.

    If the reply is helpful, please click Accept Answer and kindly upvote it. If you have additional questions about this answer, please click Comment.