0

I have an Azure function (Timer) on .NET 8.0 Isolated that gets files in a one drive folder, but I get this error:

Error: me request is only valid with delegated authentication flow.

I'm using the Microsoft Graph library version 5.61.0.

Code:

private static async Task<GraphServiceClient> GetAuthenticatedGraphClient()
{
    var tenantId = Environment.GetEnvironmentVariable("TenantId");
    var clientId = Environment.GetEnvironmentVariable("ClientId");
    var clientSecret = Environment.GetEnvironmentVariable("ClientSecret");
    var scopes = new[] { "https://graph.microsoft.com/.default" };

    var options = new ClientSecretCredentialOptions
    {
        AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
    };

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

    var graphClient = new GraphServiceClient(clientSecretCredential, scopes);

    return graphClient;
}

public async Task Run([TimerTrigger("0 */1 * * * *")] TimerInfo myTimer)
{
    _logger.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");

    var txtSourceFolder = Environment.GetEnvironmentVariable("txtSourceFolder");
    var graphClient = await GetAuthenticatedGraphClient();
    var driveID = await graphClient.Me.Drive.GetAsync();

    var driveItem = graphClient.Drives[driveID.Id]
        .Items["root"]
        .ItemWithPath(txtSourceFolder)
        .Content
        .GetAsync();

    if (myTimer.ScheduleStatus is not null)
    {
        _logger.LogInformation($"Next timer schedule at: {myTimer.ScheduleStatus.Next}");
    }
}

How can I able to get all files in a one drive folder?

1
  • You are working on an Azure function time trigger so that there's no login process in side your code, no user is signing in, then how can your app know who is Me? This is the issue, we could use .Users["user_id"] instead of .Me Commented Nov 6, 2024 at 2:34

1 Answer 1

1

According to the error message, we should use .Users["user_id"] instead of .Me as the app/sdk doesn't know who is me without a user signing in operation.

Next, Azure function is a kind of Daemon application, which only support client credential flow, and that's what you did in your app to create a client credentials provider. It requires Application type API permission and we need to set the scope as https://graph.microsoft.com/.default in codes just like what you did.

enter image description here

However this Graph API(get drive) doesn't support Application type permission, so that I'm afraid we couldn't achieve your goal with your codes. To get all files in a one drive folder, I think this API might be what you want. And it supports Application type permission which means supporting client credential flow.

enter image description here

The code shall be similar to the line below according to this guidance.

var items = await graphClient.Drives["userDriveId"].Root.ItemWithPath("").Children.GetAsync();
var items = await graphClient.Drives["userDriveId"].Items["Root"].ItemWithPath("").Children.GetAsync();

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

since we are working on client credential flow, we can only specify a user id ahead and query out the drive id of this user. Certainly, we can get the drive id from somewhere else then hardcode the id into codes. List a user's drive requires to have a user id ahead as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.