如何列出共享文件夹?

问题描述 投票:0回答:1

我想列出用户 OneDrive 中的共享文件夹。因此,我遵循了文档并致电

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

不幸的是,这给出了异常“无效请求”。我已请求范围

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

与文档相比,我看不出有什么问题。你能指点我一下吗?

感谢@Sridevi 询问错误详细信息。他们在这里。我现在看到有一个“400”响应,这似乎表明权限问题。 (奇怪的是,直到几个月前使用旧的 SDK 版本时,我才没有遇到这样的问题。)

身份验证的执行方式如下

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();

使用

 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; }
 }

所有这些代码都是使用 .net8 的 Android 应用程序的一部分(https://github.com/PhilippC/keepass2android/blob/update-onedrive-implementation/src/Kp2aBusinessLogic/Io/OneDrive2FileStorage.cs)。

exception screenshot 1

exception screenshot 2

exception screenshot 3

c# azure microsoft-graph-api onedrive
1个回答
0
投票

我的 OneDrive 帐户的 Shared 文件夹中有以下文件:

enter image description here

为了通过 Microsoft Graph API 获取这些详细信息,我在 Graph Explorer 中运行了以下 API 调用:

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

回复:

enter image description here

要通过 C# 获取相同的详细信息,您需要用户的 OneDrive ID,可以通过以下 API 调用找到该 ID:

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

回复:

enter image description here

在我的例子中,我运行了下面的 C# 代码,该代码使用 interactive flow 与 Microsoft Graph 连接并获取共享文件作为响应:

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}");
        }
    }
}

回复:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.