您好,我试图访问Office 365 Sharepoint中的某些列表。我已经设法通过较新的api调用MyFiles列表,但我相信你需要使用其他API来调用其他列表。我使用相同的访问令牌来访问文件,但下面的代码会引发未经授权的错误。有没有人对我出错的地方有任何建议?
string requestUrl = "https://mycompany.sharepoint.com/_api/Web/Lists/";
HttpClient client = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUrl);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/atom+xml"));
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
HttpResponseMessage response = await client.SendAsync(request);
if (response.IsSuccessStatusCode)
{
string responseString = await response.Content.ReadAsStringAsync();
return responseString;
}
return "Problem";
答案 0 :(得分:0)
发生身份验证错误时,Office 365将返回包含X-Ms-diagnostics
header的HTTP响应:
标题具有以下格式:
X-Ms-diagnostics = errorId ";" source ";" reason ";" fault
source
和reason
属性包含有关错误的详细信息,例如:
Reason: Token contains invalid signature.
Source: invalid_client
以下示例演示了如何检索响应错误详细信息:
using(var client = new HttpClient())
{
var request = new HttpRequestMessage(HttpMethod.Get, endpointUrl);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/atom+xml"));
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
var response = client.SendAsync(request).Result;
if (!response.IsSuccessStatusCode && response.StatusCode == HttpStatusCode.Unauthorized)
{
var diagnosticsValue = response.Headers.GetValues("x-ms-diagnostics").FirstOrDefault();
}
}