代码:BadRequest 消息:/me 请求仅在委托身份验证流程中有效

Fah*_*tar 13 c# asp.net-mvc onedrive microsoft-graph-api

我正在尝试使用microsoft graph onedrive api在 onedrive 上上传文件。我正在使用身份验证客户端凭据提供程序的方法

https://learn.microsoft.com/en-us/graph/sdks/choose-authentication-providers?tabs=CS#client-credentials-provider

喜欢:

// /.default scope, and preconfigure your permissions on the
// app registration in Azure. An administrator must grant consent
// to those permissions beforehand.
var scopes = new[] { "https://graph.microsoft.com/.default" };

// Multi-tenant apps can use "common",
// single-tenant apps must use the tenant ID from the Azure portal
var tenantId = "my-tenantid";

// Values from app registration
var clientId = "YOUR_CLIENT_ID";
var clientSecret = "YOUR_CLIENT_SECRET";

// 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 graphClient = new GraphServiceClient(clientSecretCredential, scopes);

         HttpPostedFileBase file = Request.Files;[0];
                int fileSize = file.ContentLength;
                string fileName = file.FileName;
                string mimeType = file.ContentType;
                Stream fileContent = file.InputStream;


     var res = await graphClient.Me.Drive.Root.ItemWithPath(fileName).Content
                        .Request()
                        .PutAsync<DriveItem>(fileContent);

Run Code Online (Sandbox Code Playgroud)

执行此代码后,它会给出一个错误响应。

Message: /me request is only valid with delegated authentication flow.
Inner error:
    AdditionalData:
    date: 2021-12-29T05:30:08
    request-id: b51e50ea-4a62-4dc7-b8d2-b26d75268cdc
    client-request-id: b51e50ea-4a62-4dc7-b8d2-b26d75268cdc
ClientRequestId: b51e50ea-4a62-4dc7-b8d2-b26d75268cdc
Run Code Online (Sandbox Code Playgroud)

Tin*_*ang 24

客户端凭证流将代表应用程序本身生成令牌,因此在这种情况下,用户不需要先登录为用户生成令牌,然后再调用 api。并且由于您Me在图形 SDK 中使用时的设计\xef\xbc\x8c,您的代码/应用程序不知道是谁,Me因此无法工作。你应该先知道user_id,然后在SDK中使用 ,/users/{id | userPrincipalName}代替,即/MegraphClient.Users["your_user_id"]代替graphClient.Me

\n

在您的场景中,有两种解决方案,一种方法是使用委托身份验证流程,就像您在标题中所说的那样,另一种方法是在调用图形 api 之前获取用户 ID,以便您可以使用Users["id"]但不能使用Me

\n

====================更新==========================

\n

我还没有完成代码,但我现在找到了正确的解决方案。

\n

首先,我们可以通过这个api将文件上传到一个驱动器将文件上传到一个驱动器,如果这是一个驱动器或共享点,您可以检查截图:

\n
https://graph.microsoft.com/v1.0/users/user_id/drive/items/root:/testupload2.txt:/content\n
Run Code Online (Sandbox Code Playgroud)\n

在此输入图像描述\n在此输入图像描述

\n

如果是,那么下一步很简单,使用下面的代码获取访问令牌并向调用 api 发送 http 请求:

\n
var scopes = new[] { "https://graph.microsoft.com/.default" };\nvar tenantId = "tenant_name.onmicrosoft.com";\nvar clientId = "your_azuread_clientid";\nvar clientSecret = "corresponding_client_secret";\nvar clientSecretCredential = new ClientSecretCredential(\n    tenantId, clientId, clientSecret);\nvar tokenRequestContext = new TokenRequestContext(scopes);\nvar token = clientSecretCredential.GetTokenAsync(tokenRequestContext).Result.Token;\n
Run Code Online (Sandbox Code Playgroud)\n

我知道它很复杂,因为 api 与这个不一样有 SDK 示例的 api 不一样,但我认为如果它们相似,它也值得尝试。

\n