簡體   English   中英

如何使用 v1 Azure AD 應用程序和客戶端憑據訪問 Sharepoint 在線 API

[英]How to Access Sharepoint Online API with v1 Azure AD Application and Client Credentials

我無法使用 Postman 在線對 Sharepoint 進行 API 調用。 我已經成功地對圖表 API 進行了 API 調用,所以我熟悉我認為這應該如何工作。

我已按照以下說明進行設置:

第一篇文章說它不會接受使用客戶端密碼生成的訪問令牌,但我已經生成了帶有密碼和證書的令牌,並且沒有發現任何區別。

調用任何東西時,例如:

https://<tenant>.sharepoint.com/_api/web

我得到錯誤:

{"error_description":"Exception of type 'Microsoft.IdentityModel.Tokens.AudienceUriValidationFailedException' was thrown."}

這是我使用 v1 /token 端點生成的訪問令牌示例:

{
  "aud": "https://microsoft.sharepoint-df.com/",
  "iss": "https://sts.windows.net/462c0b***********c3708/",
  "iat": 1569243291,
  "nbf": 1569243291,
  "exp": 1569247191,
  "aio": "42FgYDiXt***********==",
  "app_displayname": "T***********n",
  "appid": "00c***********2b",
  "appidacr": "2",
  "idp": "https://sts.windows.net/46***********708/",
  "oid": "2f8a5***********684",
  "roles": [
    "User.ReadWrite.All",
    "TermStore.Read.All",
    "Sites.FullControl.All"
  ],
  "sid": "5ab8d57***********0bc",
  "sub": "2f8a5***********684",
  "tid": "462c0***********708",
  "uti": "aHt8d***********9AA",
  "ver": "1.0"
}

正如您在第一篇文章中看到的,使用客戶端密鑰生成的訪問令牌不支持 SharePoint 的僅應用令牌身份驗證。

如果你解碼訪問令牌,你會發現對於“appidacr”,如果使用客戶端ID和客戶端密碼,則值為“1”。 如果使用客戶端證書進行身份驗證,則值為“2”。 在此處查看詳細信息。

客戶端證書比客戶端密碼更安全。 它提供雙重驗證和保護。

您可以參考這篇第 3 方文章,使用證書獲取 Azure AD 僅限應用訪問令牌,並使用此訪問令牌訪問您的 SharePoint 資源。

當然,您在評論中提到的文章也很有幫助。 它使用 ACS 來完成身份驗證。

該錯誤消息似乎暗示我在 /token 端點調用中的資源參數設置不正確。 我相信微軟文檔中省略了這一點,因為文檔是如此分裂。 V1 應用程序調用 SharePoint Online 的正確令牌端點調用如下所示:

Web 服務

POST https://login.microsoftonline.com/<TARGET-TENANT-ID OR NAME>/oauth2/token

參數

client_id= <Application ID from Azure Portal>
grant_type=client_credentials
resource= https://<TARGET-TENANT-NAME>.sharepoint.com
client_assertion_type= urn:ietf:params:oauth:client-assertion-type:jwt-bearer
client_assertion= <See Link Above to create assertion>

上面的鏈接省略了如何計算證書 JWT 的 x5t 值。 你可以使用這個:

echo $(openssl x509 -in certificate.pem -fingerprint -noout) | sed 's/SHA1 Fingerprint=//g' | sed 's/://g' | xxd -r -ps | base64

我從這里得到的: How to get value of "x5t" using Certificate credentials for application authentication

如果您嘗試使用客戶端密碼而不是客戶端斷言,您將獲得一個令牌,但 SharePoint 在線 REST API 將返回:

Unsupported app only token.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM