SharePoint Rest API如何获取访问令牌?

时间:2020-08-09 00:57:54

标签: sharepoint microsoft-graph-api sharepoint-online

只是开始使用SharePoint和Microsoft身份验证,然后尝试将SharePoint列表添加到JavaScript应用程序中。从Microsoft文档中,我需要使用以下内容:

GET https://{site_url}/_api/web/lists/GetByTitle('List Title')
Authorization: "Bearer " + accessToken
Accept: "application/json;odata=verbose"

已到处搜索以找到有关如何获取此accessToken的明确答案。我从Microsoft可以找到的所有文档似乎都已过时。有人知道当前获取accessToken的方法吗?

2 个答案:

答案 0 :(得分:0)

您可以参考本文以获得访问令牌:

https://global-sharepoint.com/sharepoint-online/in-4-steps-access-sharepoint-online-data-using-postman-tool/

Post https://accounts.accesscontrol.windows.net/<Tenant ID>/tokens/OAuth/2

身体:

grant_type     client_credentials
client_id      <Client ID>
client_secret  <Client Secret>
resource       00000003-0000-0ff1-ce00-000000000000/<tenant>.sharepoint.com@<Tenant ID>

我的测试结果:

enter image description here

答案 1 :(得分:0)

要调用SharePoint特定的API,您需要获取SPO特定的访问令牌。您可以通过执行以下操作来“交换” SPO特定令牌的常规MS Graph刷新令牌:

  1. 像往常一样从图获取委托的身份验证令牌 (https://docs.microsoft.com/en-us/graph/auth-v2-user
  2. 使用您获得的refresh_token并通过再次调用auth端点将其交换为SPO访问令牌:
POST https://login.microsoftonline.com/{{tenantName}}/oauth2/v2.0/token

具有以下表单数据:

client_id=<APP ID>
client_secret=<APP SECRET>
refresh_token=<REFRESH TOKEN FROM ABOVE>
grant_type=refresh_token
scope=https://<YOUR TENANT NAME>.sharepoint.com/Sites.Read.All
  1. 获取访问令牌并调用SPO API

您必须确保已使用正确的权限注册了您的应用。在上述情况下,该应用必须具有Sites.Read.All。