Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Office 365 OAuth API对SharePoint端点进行身份验证_Sharepoint_Office365 - Fatal编程技术网

使用Office 365 OAuth API对SharePoint端点进行身份验证

使用Office 365 OAuth API对SharePoint端点进行身份验证,sharepoint,office365,Sharepoint,Office365,我正在尝试使用提到的Office 365 API访问sharepoint在线网站 我正在获取身份验证令牌并调用发现服务,如下所示: httpClient = new HttpClient(); httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authResult.AccessToken); httpClient.DefaultRequestHeaders.Add("A

我正在尝试使用提到的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:

  • OneDrive
    https://sometenant-my.sharepoint.com/personal/sometenant\u data\u onmicrosoft\u com//api

  • 与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数据

    我已授予应用程序对所有网站集权限的完全控制权


    请告知我是否遗漏了任何内容。

    最初尝试获取访问令牌时,您的目标URL与SharePoint所需的不同。我不知道为什么,Office365 access\u令牌在SharePoint上运行似乎是合乎逻辑的,但事实并非如此

    因此,我假设您在SharePoint注册的应用程序中有客户id客户机密。如果没有,则有两种方法可以注册新应用程序:

  • https://{your tenantID}.sharepoint.com//\u layouts/15/appregnew.aspx
  • (由于某些原因,Azure ACS在尝试获取acess_令牌时未验证此处生成的客户端_密钥,至少对我来说不是。因此我尝试了下面的一个)

  • 登录到azure管理门户并转到:
  • active directory(左侧底部)>默认目录(如果您没有 以前有过)>应用程序>添加

    在这里填写应用程序的详细信息,应用程序ID URI='https://{your tenantID}.sharepoint.com/',在底部“对其他应用程序的权限”“别忘了添加应用程序>Office 365 sharepoint Online”

    获取授权码:

    https://{tenantID}.sharepoint.com/_layouts/oauthauthorize.aspx?客户端id={ur 客户 id}&scope=Web.Read&response\u type=code&redirect\u uri=https%3A%2F%2folocalhost%2F

    在chrome中键入上述url并输入,您将被重定向到上面指定的url。你最终会在

    “{授权码}”

    复制授权码

    获取承载域:

    获取请求

    https://{your tenantID}.sharepoint.com//\u vti\u bin/client.svc

    授权:持票人(抬头)

    从响应头获取承载域组件并保存它

    获取访问令牌:

    后请求

    {承载域}/tokens/OAuth/2

    和车身参数

    授权类型=授权代码和客户端id={ur客户端 id}&client_secret={ur client secret}&code={您从 台阶 上面}&redirect_uri=https%3A%2F%2F本地主机%2F&resource=00000003-0000-0ff1-ce00-000000000000000000%2F{您的 tenantID}.sharepoint.com%40{bearer realm}

    &resource=0000000 3-0000-0ff1-ce00-000000000000对于sharepoint是永久的


    这将返回一个带有访问令牌和刷新令牌的响应,现在使用此令牌,您将能够访问SharePoint REST API。

    您是如何使用此令牌的?找到了任何好的示例,或者您使其工作了吗?