【问题标题】:Code: BadRequest Message: /me request is only valid with delegated authentication flow代码:BadRequest 消息:/me 请求仅对委托身份验证流程有效
【发布时间】:2022-01-27 14:52:51
【问题描述】:

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

https://docs.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://docs.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);

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

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

【问题讨论】:

    标签: c# asp.net-mvc microsoft-graph-api onedrive


    【解决方案1】:

    客户端凭证流将代表应用程序本身生成令牌,因此在这种情况下,用户无需先登录为用户生成令牌,然后调用api。而且由于设计的原因,当你在graph SDK中使用Me时,你的代码/应用不知道Me是谁,所以它不能工作。你应该先知道user_id,在SDK中使用/users/{id | userPrincipalName}而不是/Me,即graphClient.Users["your_user_id"]而不是graphClient.Me

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

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

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

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

    https://graph.microsoft.com/v1.0/users/user_id/drive/items/root:/testupload2.txt:/content
    

    如果是,那么接下来就简单了,使用下面的代码获取访问令牌并发送http请求调用api:

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

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

    【讨论】:

    • 谢谢:我已经完成了GraphServiceClient 身份验证,也成功上传了文件,但现在问题是我上传的文件转到了 sharepoint 通信站点。但我想上传到 onedrive 而不是 sharepoint 通信网站。所以请你解释一下我的观点问题。
    猜你喜欢
    • 1970-01-01
    • 2019-07-17
    • 2019-09-15
    • 2019-04-06
    • 1970-01-01
    • 2019-03-10
    • 2023-03-25
    • 2020-07-26
    • 1970-01-01
    相关资源
    最近更新 更多