gpt4 book ai didi

c# - 使用 OneDrive API 的正确方法是什么?

转载 作者:行者123 更新时间:2023-12-03 02:36:44 36 4
gpt4 key购买 nike

我想在我的 WinForms 应用程序中与 OneDrive 交互。遗憾的是,Azure 快速入门示例不包括 WinForms,仅包括 UWD。

我必须做的事情的流程是一致的,即给定我的 Client ID ,我必须获得 Authentication Code 。给定身份验证码,我可以获得 Access Code ,这将允许我以 RESTful 方式与 OneDrive 进行交互API。我的计划是将认证件放入 .Net Framework Library文件 IO 调用将进入另一个没有用户界面访问权限的库,就像它进入 Windows 服务一样。我会将访问 token 传递给服务。

AADSTS50059: No tenant-identifying information found in either the request or implied by any provided credentials.

此错误对应于我从示例 .Net Core 守护程序快速启动代码中提取的以下代码片段。

注意:我正在玩Scopes因为我不断收到范围错误,并且看到一篇文章,我应该保留其链接,其中指出使用 API 和默认范围。

public bool GetRestAuthenticationToken(out string tokenAuthentication)
{
tokenAuthentication = null;
try
{
IConfidentialClientApplication app;
app = ConfidentialClientApplicationBuilder.Create(Authenticate.AppClientId)
.WithClientSecret(Authenticate.AppClientSecret)
.WithAuthority(new Uri(@"https://login.microsoftonline.com/common/oauth2/nativeclient"))
.Build();


string scope = $"onedrive.readwrite offline_access";
System.Collections.Generic.List<string> enumScopes = new System.Collections.Generic.List<string>();
enumScopes.Add("api://<GUID>/.default");
//enumScopes.Add(Authenticate.Scopes[1]);
var result = Task.Run(async () => await app.AcquireTokenForClient(enumScopes).ExecuteAsync()).Result;
...
}
...
}

我相信我现在已经在 Azure 上正确配置了我的应用程序,但我并不 100% 肯定。

API 权限:

Registered App on Azure Portal Showing API Permissions

身份验证:

其他:

  • 我确实有一个客户 secret ,并记录了所有概述 GUIDs

Microsoft Doc 1

我尝试了几个不同的 URL,但只有未注释掉的 URL 适用于上面的片段,但会引发引用的错误。

            //string redirect_uri = @"https://www.myapp.com/auth";
//string redirect_uri = "https://login.live.com/oauth20_desktop.srf";
string url = @"https://login.microsoftonline.com/common/oauth2/nativeclient";
//string url = $"https://login.live.com/oauth20_authorize.srf?client_id={appClientId}&scope={scope}&response_type=code&redirect_uri={redirect_uri}";
//string url = $"https://login.microsoftonline.com/common/oauth2/v2.0/authorize?" +
// $"client_id={Authenticate.AppClientId}&" +
// $"scope={scope}&" +
// $"response_type=token&" +
// $"redirect_uri={redirect_uri}";

目标是相同的,即获得可以与 RESTful 一起使用的访问 token 调用以处理 OneDrive 上的文件和/或目录,例如

            System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
client.GetAsync(...);

最佳答案

您正在尝试实现客户端凭据授予类型来获取访问 token 。

基于MSAL initialization权限

(Optional) The STS endpoint for user to authenticate. Usuallyhttps://login.microsoftonline.com/{tenant} for public cloud, where{tenant} is the name of your tenant or your tenant Id.

我们假设您的租户是“myTenent.onmicrosoft.com”,那么您应该在此处将其设置为 https://login.microsoftonline.com/myTenent.onmicrosoft.com

我注意到您在代码中指定了范围“onedrive.readwrite”。但这不是 Microsoft Graph 的有效权限。 Microsoft Graph 的默认范围是 https://graph.microsoft.com/.default

关于c# - 使用 OneDrive API 的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62905771/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com