使用Office 365 OAuth API进行SharePoint终端的身份验证

3

我正在尝试使用此处提到的Office 365 API访问SharePoint在线站点:这里。我已获取授权令牌并按如下调用了发现服务:

httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authResult.AccessToken);
httpClient.DefaultRequestHeaders.Add("Accept", "application/json; odata=verbose");
response = await httpClient.GetAsync(new Uri("https: / /api.office.com/discovery/me/services"));
data = await response.Content.ReadAsStringAsync();

在结果中,我得到了以下类型的终端点URL:

  1. OneDrive
    https: / / sometenant-my.sharepoint.com/personal/sometenant_data_onmicrosoft_com/_api

  2. 与Outlook相关的
    https: / /outlook.office365.com/api/v1.0

但是,在结果中我没有得到任何SharePoint的终端点URL。 如果我尝试以下代码:

 httpClient = new HttpClient();
 httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authResult.AccessToken);
 httpClient.DefaultRequestHeaders.Add("Accept", "application/json; odata=verbose");
 response = await httpClient.GetAsync("https://sometenant.sharepoint.com/_api/web/lists/getByTitle('Documents')/items");
 data = await response.Content.ReadAsStringAsync();

我在响应流中得到以下内容:
"{\"error\":\"invalid_client\",\"error_description\":\"Invalid audience Uri 'Microsoft.SharePoint'.\"}"

响应中的错误是:

{StatusCode: 401, ReasonPhrase: 'Unauthorized', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  x-ms-diagnostics: 3000003;reason="Invalid audience Uri 'Microsoft.SharePoint'.";category="invalid_client"
  SPRequestGuid: 8462cf9c-c093-1000-a3da-fc5e1aab16c1
  request-id: 8462cf9c-c093-1000-a3da-fc5e1aab16c1
  SPRequestDuration: 37
  SPIisLatency: 25
  MicrosoftSharePointTeamServices: 16.0.0.3431
  X-Content-Type-Options: nosniff
  X-MS-InvokeApp: 1; RequireReadOnly
  Date: Mon, 24 Nov 2014 22:45:46 GMT
  P3P: CP="ALL IND DSP COR ADM CONo CUR CUSo IVAo IVDo PSA PSD TAI TELo OUR SAMo CNT COM INT NAV ONL PHY PRE PUR UNI"
  Server: Microsoft-IIS/7.5
  WWW-Authenticate: Bearer realm="xxxxxx-xxxx-xxxxx-xxxx-xxxxxxxx",client_id="xxxxxxxx-xxx-xxxx-xxxx-000000000000",trusted_issuers="xxxxxxx-xxxx-xxx-xxx-000000000000@*,https : // sts.windows.net/*/,00000003-0000-xxxxx-ce00-000000000000@xxxxxxxx-xxxxx-11e1-xxxx-xxxxxxx",authorization_uri="https://login.windows.net/common/oauth2/authorize"
  X-Powered-By: ASP.NET
  Content-Length: 93
}}

我相信我应该能够使用Office 365 API访问SharePoint数据。

我已经在所有网站集权限上给了应用程序完全控制。

请告知我是否有什么遗漏。


你是如何解决这个问题的?找到了好的样本还是自己让它运行起来了? - Jorge
1个回答

2
您在尝试获取访问令牌时使用的目标URL最初与SharePoint所需的不同。我不知道为什么,似乎Office365的访问令牌可以用于SharePoint,但事实并非如此。
因此,我假设您已经从SharePoint注册应用程序中获得了client_id和client_secret。如果没有,您可以有两种方法注册新应用程序:
1. https://{your tenantID}.sharepoint.com/_layouts/15/appregnew.aspx
(由于某种原因,在这里生成的client_secret无法通过Azure ACS进行验证以获取访问令牌,至少对我而言是这样。因此,我尝试了下面的方法)
2. 登录到Azure管理门户,并转到:
活动目录(左侧底部)>默认目录(如果您之前没有)>应用程序>添加
在此填写您的应用程序细节,APP ID URI = 'https://{您的租户ID}.sharepoint.com/',在底部的“向其他应用程序授予权限”中不要忘记添加应用程序 > Office 365 SharePoint Online。 获取授权码: 将上述网址输入 Chrome 并按 Enter 键,您将被重定向到您指定的网址。最终,您将会到达:
https://localhost/?code={authorization code}”
复制授权码。 获取承载域: GET 请求 "https://{您的租户ID}.sharepoint.com/_vti_bin/client.svc"
Authorization: Bearer(头)
从响应头中获取承载域组件并保存。
获取访问令牌:
POST请求
https://accounts.accesscontrol.windows.net/{Bearer 域名}/tokens/OAuth/2
请求体参数:
grant_type=authorization_code&client_id={你的客户端 ID}&client_secret={你的客户端密钥}&code={你从上一步获取的授权码}&redirect_uri=https%3A%2F%2Flocalhost%2F&resource=00000003-0000-0ff1-ce00-000000000000%2F{你的租户ID}.sharepoint.com%40{Bearer 域名}
其中,&resource = 00000003-0000-0ff1-ce00-000000000000 是 SharePoint 的固定值。
这样应该会返回一个访问令牌和刷新令牌的响应。现在使用这个令牌即可访问 SharePoint REST API。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接