【发布时间】:2021-10-22 03:32:06
【问题描述】:
我有一个 WPF/C# 应用程序,我正在尝试将文件上传到 SharePoint 网站的子文件夹。
我已关注this example,首先在 Azure Active Directory 中注册了我的应用程序。这一步是必要的,因为我想在我的应用程序中使用 Microsoft 的身份服务,并避免让用户每次都输入用户名和密码。相反,Microsoft 将返回一个访问令牌,然后我可以使用它来查询 Graph API 和 SharePoint Online。
我使用以下设置注册了应用程序:
- 支持的帐户类型:任何组织目录(任何 Azure AD 目录 - 多租户)中的帐户和个人 Microsoft 帐户(例如 Skype、Xbox)
- 身份验证:移动和桌面应用程序
- 重定向 URI:https://login.microsoftonline.com/common/oauth2/nativeclient
另外,我这样设置 API 权限: screenshot
在我的代码中,我添加了 MSAL 包和 Microsoft Graph 包。下面是获取访问令牌的代码(我从上面链接的同一个 Microsoft Doc 帖子中遵循)。
这是我获取授权令牌的方式:
var authResult = await PublicClientApp.AcquireTokenInteractive(scopes)
.WithAccount(accounts.FirstOrDefault())
.WithPrompt(Microsoft.Identity.Client.Prompt.SelectAccount)
.ExecuteAsync();
作用域在哪里:
string[] scopes = new string[] { "user.read" };
然后,在获得身份验证后,我尝试使用获取 SharePoint 网站的 ID 和驱动器 ID,如 this example 中所示。
string site = "mytenant.sharepoint.com";
string relativePath = "/sites/mytargetsite";
if (authResult != null)
{
var authProvider = new DelegateAuthenticationProvider(async (request) =>
{
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", authResult.AccessToken);
});
GraphServiceClient graphClient = new GraphServiceClient(authProvider);
var s = await graphClient
.Sites[this.site]
.SiteWithPath(this.relativePath)
.Request()
.GetAsync();
var d = await graphClient
.Sites[s.Id]
.Drive
.Request()
.GetAsync();
}
上传文件的代码还有很多,但此时我的代码失败了。我得到的错误是:
“Microsoft.Graph.ServiceException:'代码:invalidRequest 消息:此租户的主机名无效...”
这就是我卡住的地方。我正在伸出手,看看是否有人对我做错了什么有线索。谢谢!
【问题讨论】:
标签: c# wpf microsoft-graph-api sharepoint-online msal