【发布时间】:2021-02-05 13:02:47
【问题描述】:
简短说明:
我尝试从我的测试共享点查询站点:{my_name}.sharepoint.com 使用来自 http 请求的本机 REST 接口以及使用 Graph SDK。身份验证很好,我可以使用这两种方法获取令牌。我已经在 portal.azure.com 上进行了应用注册、授予权限并为他们提供了管理员同意。
身份验证:
http请求代码:
FormUrlEncodedContent content = new FormUrlEncodedContent(new[] {
new KeyValuePair<string, string>("client_id", $"{ClientId}"),
new KeyValuePair<string, string>("scope", "https://graph.microsoft.com/.default"),
new KeyValuePair<string, string>("grant_type", "client_credentials"),
new KeyValuePair<string, string>("client_secret", ClientSecret)
});
string url = $"https://login.microsoftonline.com/{TenantId}/oauth2/v2.0/token";
Console.WriteLine(url);
var message = new HttpRequestMessage(HttpMethod.Post, url);
message.Content = content;
message.Content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
message.Headers.Accept.Clear();
message.Headers.Accept.TryParseAdd("application/json");
var response = await httpClient.SendAsync(message);
图形 SDK 代码:
IConfidentialClientApplication app =
ConfidentialClientApplicationBuilder
.Create(clientId)
.WithClientSecret(clientSecret)
.WithAuthority(new Uri($"https://login.microsoftonline.com/{GetWWWAuthResponseHeaders(domain)["Bearer realm"]}")
).Build();
var authenticationResult = await app.AcquireTokenForClient(new string[] { "https://graph.microsoft.com/.default" }).ExecuteAsync();
var graphClient = new GraphServiceClient(
new DelegateAuthenticationProvider(requestMessage => {
requestMessage.Headers.Authorization =
new AuthenticationHeaderValue("bearer", authenticationResult.AccessToken);
return Task.FromResult(0);
})
);
产生错误的请求:
原生 http 代码:
string url = $"https://graph.microsoft.com/v1.0/sites/{Domain}:/sites/{Site}";
Console.WriteLine($"url: {url}");
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, url);
request.Headers.Add("Authorization", $"Bearer {Token.Token}");
request.Headers.Add("Accept", "application/json");
HttpResponseMessage response = await httpClient.SendAsync(request);
responseBody = await response.Content.ReadAsStringAsync();
图形 SDK 代码:
var site = graphClient.Sites[$"{my_name}.sharepoint.com"].Request().GetAsync().Result;
我得到的确切错误:
ServiceException: Code: generalException
Message: An unspecified error has occurred.
Inner error:
AdditionalData:
date: 2021-02-05T10:02:19
request-id: a3567eca-3d3b-4617-b877-e8f7369660b3
client-request-id: a3567eca-3d3b-4617-b877-e8f7369660b3
ClientRequestId: a3567eca-3d3b-4617-b877-e8f7369660b3
【问题讨论】:
-
Baliant,在您的应用程序之外尝试使用具有相同 Graph API 调用的 POSTMAN/Graph Explorer 重现该问题,看看您是否仍然可以重现该问题。我试过了,但我没能在最后重现这个问题。
-
当您使用客户端凭据授予时,获取该令牌并使用 curl 或 POSTMAN 调用此 Graph API
https://graph.microsoft.com/v1.0/sites/domain.sharepoint.com并查看结果。 -
我也从 Postman 收到同样的 500 Internal Server 错误。根据集合,我可以从login.microsoftonline.com{{TenantID}}/oauth2/v2.0/token 获取令牌,我正在调用graph.microsoft.com/v1.0/sites{{Domain}}.sharepoint.com 并获得500。我在 jwt.io 上检查了我的令牌,我觉得它看起来不错,如果我故意弄乱令牌,我会得到 401,如你所料。
标签: sharepoint microsoft-graph-api microsoft-graph-sdks