我试图使用Office365个API访问sharepoint站点,其中提到了这里,我得到了auth令牌,并按如下方式调用了发现服务:
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:
我在结果中没有得到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
}}我相信我应该能够使用Office365个API访问SharePoint数据。
我已经给予所有网站收集权限的应用程序完全控制。
如果我在这里遗漏了什么,请告诉我。
发布于 2016-05-31 09:43:36
您的目标URL最初在尝试获取access_token时与SharePoint所需的URL不同。我不知道为什么,Office365 access_token为SharePoint工作似乎合乎逻辑,但事实并非如此。
因此,我假设您的注册应用程序中有client_id和client_secret。如果没有,可以通过两种方式注册一个新应用程序:
(出于某种原因,这里生成的client_secret在尝试获取acess_token时没有被Azure验证,至少对我来说并非如此。所以我尝试了下面的一个)
活动目录(左侧底部)>默认目录(如果您以前没有)> Application > Add
这里填写应用程序的详细信息,应用程序ID URI = 'https://{your tenantID}.sharepoint.com/‘,在“其他应用程序的权限”中,不要忘记添加应用程序> Office 365 SharePoint Online。
获得授权代码的 :
id}&scope=Web.Read&response_type=code&redirect_uri=https%3A%2F%2Flocalhost%2F客户端id={ur
键入上面的url,然后输入,您将被重定向到上面指定的url。你最终会在
“https://localhost/?code={授权代码}”
复制授权代码
获得承载域的 :
索取请求
https://{your tenantID}.sharepoint.com/_vti_bin/client.svc
授权:无记名(报头)
从响应头获取Bearer领域组件并保存它。
获取访问令牌的 :
员额请求
https://accounts.accesscontrol.windows.net/{承载域}/令牌/OAuth/2
和身体参数
从step above}&redirect_uri=https%3A%2F%2Flocalhost%2F&resource=00000003-0000-0ff1-ce00-000000000000%2F{your tenantID}.sharepoint.com%40{承载域获得的grant_type=authorization_code&client_id={ur client id}&client_secret={ur client机密}&code={auth代码
&resource = 00000003-0000-0ff1-ce00-000000000000对于sharepoint是永久的。
这应该会返回一个带有访问令牌&刷新令牌的响应,现在使用它,您将能够访问SharePoint REST。
https://stackoverflow.com/questions/27115740
复制相似问题