c# - How to list shared folders? - Stack Overflow

admin2025-04-22  1

I want to list the shared folders in a user's OneDrive. I therefore followed the docs and called

var sharedWithMeResponse = await client.Drives[driveId].SharedWithMe.GetAsSharedWithMeGetResponseAsync();

Unfortunately this gives an exception "Invalid request". I have requested scopes

{
    ".ReadWrite",
    ".ReadWrite.All"
}

I don't see what could be wrong compared to the documentation. Can you point me to it?

Thanks @Sridevi for asking for error details. Here they are. I now see that there is a "400" response which seems to indicate permission issues. (Weirdly, until few months ago with the old SDK version, I didn't have such problems.)

The Authentication is performed like

var _publicClientApp = PublicClientApplicationBuilder.Create(ClientID)
    .WithRedirectUri($"msal{ClientID}://auth")
    .Build();
...

var scopes = new List<string>
                    {
                        ".ReadWrite",
                        ".ReadWrite.All"
                    };

AuthenticationResult authenticationResult = await _publicClientApp.AcquireTokenInteractive(scopes)
    .WithParentActivityOrWindow((Activity)activity)
    .ExecuteAsync();
var authenticationProvider = new BaseBearerTokenAuthenticationProvider(new TokenFromAuthResultProvider() { AuthenticationResult = authenticationResult });
var client = new GraphServiceClient(new HttpClient(), authenticationProvider);

//this client is then used for the call which fails:
var sharedWithMeResponse = await client.Drives[driveId].SharedWithMe.GetAsSharedWithMeGetResponseAsync();

which uses

 public class TokenFromAuthResultProvider : IAccessTokenProvider
 {
     public AuthenticationResult AuthenticationResult
     {
         get;
         set;
     }
     public async Task<string> GetAuthorizationTokenAsync(Uri uri, Dictionary<string, object>? additionalAuthenticationContext = null,
         CancellationToken cancellationToken = new CancellationToken())
     {
         return AuthenticationResult.AccessToken;
     }

     public AllowedHostsValidator AllowedHostsValidator { get; }
 }

All of this code is part of an Android app using 8 (.cs).

I want to list the shared folders in a user's OneDrive. I therefore followed the docs and called

var sharedWithMeResponse = await client.Drives[driveId].SharedWithMe.GetAsSharedWithMeGetResponseAsync();

Unfortunately this gives an exception "Invalid request". I have requested scopes

{
    "https://graph.microsoft.com/Files.ReadWrite",
    "https://graph.microsoft.com/Files.ReadWrite.All"
}

I don't see what could be wrong compared to the documentation. Can you point me to it?

Thanks @Sridevi for asking for error details. Here they are. I now see that there is a "400" response which seems to indicate permission issues. (Weirdly, until few months ago with the old SDK version, I didn't have such problems.)

The Authentication is performed like

var _publicClientApp = PublicClientApplicationBuilder.Create(ClientID)
    .WithRedirectUri($"msal{ClientID}://auth")
    .Build();
...

var scopes = new List<string>
                    {
                        "https://graph.microsoft.com/Files.ReadWrite",
                        "https://graph.microsoft.com/Files.ReadWrite.All"
                    };

AuthenticationResult authenticationResult = await _publicClientApp.AcquireTokenInteractive(scopes)
    .WithParentActivityOrWindow((Activity)activity)
    .ExecuteAsync();
var authenticationProvider = new BaseBearerTokenAuthenticationProvider(new TokenFromAuthResultProvider() { AuthenticationResult = authenticationResult });
var client = new GraphServiceClient(new HttpClient(), authenticationProvider);

//this client is then used for the call which fails:
var sharedWithMeResponse = await client.Drives[driveId].SharedWithMe.GetAsSharedWithMeGetResponseAsync();

which uses

 public class TokenFromAuthResultProvider : IAccessTokenProvider
 {
     public AuthenticationResult AuthenticationResult
     {
         get;
         set;
     }
     public async Task<string> GetAuthorizationTokenAsync(Uri uri, Dictionary<string, object>? additionalAuthenticationContext = null,
         CancellationToken cancellationToken = new CancellationToken())
     {
         return AuthenticationResult.AccessToken;
     }

     public AllowedHostsValidator AllowedHostsValidator { get; }
 }

All of this code is part of an Android app using .net8 (https://github.com/PhilippC/keepass2android/blob/update-onedrive-implementation/src/Kp2aBusinessLogic/Io/OneDrive2FileStorage.cs).

Share Improve this question edited Jan 22 at 6:16 DarkBee 15.5k8 gold badges72 silver badges118 bronze badges asked Jan 21 at 15:18 PhilippPhilipp 11.8k9 gold badges71 silver badges129 bronze badges 4
  • Could you include complete error details and what authentication flow you are currently using? – Sridevi Commented Jan 22 at 4:05
  • @Sridevi I have updated the question with more details. Thanks for asking! – Philipp Commented Jan 22 at 6:15
  • 400 doesn't indicate an issue with authentication. Seems like the SDK is creating wrong request URL – user2250152 Commented Jan 22 at 8:28
  • Hi @Philipp Any update? – Sridevi Commented Jan 24 at 9:23
Add a comment  | 

1 Answer 1

Reset to default 2

I have below file in Shared folder of my OneDrive account:

To get these details via Microsoft Graph API, I ran below API call in Graph Explorer:

GET https://graph.microsoft.com/v1.0/me/drive/sharedWithMe

Response:

To fetch the same details via c#, you need user's OneDrive ID that can be found with below API call:

GET https://graph.microsoft.com/v1.0/me/drive/

Response:

In my case, I ran below c# code that uses interactive flow to connect with Microsoft Graph and fetched shared files in response:

using Azure.Identity;
using Microsoft.Graph;
using Microsoft.Graph.Models.ODataErrors;

class Program
{
    static async Task Main(string[] args)
    {
        var tenantId = "tenantId";
        var clientId = "appId";

        var scopes = new[] { "https://graph.microsoft.com/Files.ReadWrite",
    "https://graph.microsoft.com/Files.ReadWrite.All" }; 
        var interactiveCredential = new InteractiveBrowserCredential(new InteractiveBrowserCredentialOptions
        {
            TenantId = tenantId,
            ClientId = clientId,
            AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
            RedirectUri = new Uri("http://localhost") //Use your redirect URI
        });

        var graphClient = new GraphServiceClient(interactiveCredential, scopes);

        var driveId = "OneDriveId";

        try
        {
            var result = await graphClient.Drives[driveId].SharedWithMe.GetAsSharedWithMeGetResponseAsync();

            if (result?.Value != null && result.Value.Count > 0)
            {
                foreach (var item in result.Value)
                {
                    Console.WriteLine($"Name: {item.Name}");
                    Console.WriteLine($"ID: {item.Id}");
                    Console.WriteLine($"Web URL: {item.WebUrl}");
                    Console.WriteLine($"Last Modified: {item.LastModifiedDateTime}");
                    Console.WriteLine(new string('-', 40));
                }
            }
            else
            {
                Console.WriteLine("No shared items found.");
            }
        }
        catch (ODataError odataError)
        {
            Console.WriteLine($"Error Code: {odataError.Error?.Code}");
            Console.WriteLine($"Error Message: {odataError.Error?.Message}");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Exception: {ex.Message}");
        }
    }
}

Response:

转载请注明原文地址:http://anycun.com/QandA/1745301359a90580.html