简体   繁体   English

如何使用 v1 Azure AD 应用程序和客户端凭据访问 Sharepoint 在线 API

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

I am unable to make an API call to Sharepoint Online using Postman.我无法使用 Postman 在线对 Sharepoint 进行 API 调用。 I have successfully made API calls to the Graph API so I am familiar with how I think this should work.我已经成功地对图表 API 进行了 API 调用,所以我熟悉我认为这应该如何工作。

I have followed these instructions for setup:我已按照以下说明进行设置:

The first article says that it won't accept access tokens generated using a client secret, but I have generated tokens with a secret and a certificate and have found no difference.第一篇文章说它不会接受使用客户端密码生成的访问令牌,但我已经生成了带有密码和证书的令牌,并且没有发现任何区别。

When calling anything, such as:调用任何东西时,例如:

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

I get the error:我得到错误:

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

here's a sample of the access token I generate with the v1 /token endpoint:这是我使用 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"
}

As you see in the first article, access tokens generated using a client secret is not-supported App only token authentication for SharePoint.正如您在第一篇文章中看到的,使用客户端密钥生成的访问令牌不支持 SharePoint 的仅应用令牌身份验证。

If you decode the access token, you will find that for "appidacr", if client ID and client secret are used, the value is "1".如果你解码访问令牌,你会发现对于“appidacr”,如果使用客户端ID和客户端密码,则值为“1”。 If a client certificate was used for authentication, the value is "2".如果使用客户端证书进行身份验证,则值为“2”。 See details here . 在此处查看详细信息。

Client certificate is more more secure than client secret.客户端证书比客户端密码更安全。 It provides dual verification and protection.它提供双重验证和保护。

You can refer to this 3rd-party article to get Azure AD app-only access token using certificate and use this access token to access your SharePoint resources.您可以参考这篇第 3 方文章,使用证书获取 Azure AD 仅限应用访问令牌,并使用此访问令牌访问您的 SharePoint 资源。

Of course, the article you mentioned in the comment is also helpful.当然,您在评论中提到的文章也很有帮助。 It uses ACS to finish authentication.它使用 ACS 来完成身份验证。

The error message seems to imply that my resource parameter in the /token endpoint call was set incorrectly.该错误消息似乎暗示我在 /token 端点调用中的资源参数设置不正确。 I believe this is omitted in the Microsoft documentation, since the documentation is so divided.我相信微软文档中省略了这一点,因为文档是如此分裂。 The correct token endpoint call for a V1 app to call SharePoint Online looks like the following: V1 应用程序调用 SharePoint Online 的正确令牌端点调用如下所示:

Web Service Web 服务

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

Parameters参数

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>

The links above omit how to calculate the x5t value for the certificate JWT.上面的链接省略了如何计算证书 JWT 的 x5t 值。 You can use this:你可以使用这个:

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

which I got from here: How to obtain value of "x5t" using Certificate credentials for application authentication我从这里得到的: How to get value of "x5t" using Certificate credentials for application authentication

If you try to use client secret instead of client assertion, you'll get a token back, but the SharePoint Online REST API will return:如果您尝试使用客户端密码而不是客户端断言,您将获得一个令牌,但 SharePoint 在线 REST API 将返回:

Unsupported app only token.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Azure AD v2.0 身份验证的 Sharepoint Online REST API - Sharepoint Online REST API with Azure AD v2.0 authentication Azure AD安全组对SharePoint Online的访问 - Azure AD Security Group access to SharePoint Online 如何在 azure function v1 中使用最新的 SharePoint PnP Core Online - How to use latest SharePoint PnP Core Online in azure function v1 通过 Azure AD 使用客户端凭据保护 API - Securing API with client credentials via Azure AD 是否可以使用Azure AD身份验证访问SharePoint Online数据? - Is It Possible To Access SharePoint Online Data Using Azure AD Authentication? 使用client_credentials Azure AD Graph API进行授权时出现问题 - Trouble with authorization using client_credentials Azure AD Graph API 我可以使用 API 生成 Azure AD 客户端凭据机密吗? - Can I generate Azure AD Client Credentials Secrets using an API? 如何查询 Sharepoint REST api (不是图形 api) 使用 Z3A580F1422038F1422038673F1 注册应用程序? - How to query the Sharepoint REST api (not the Graph api) using an Azure AD registered application? 授权Azure AD应用程序访问RateCard API - Authorize Azure AD application to access RateCard API 应用程序注册中缺少Azure USGov Sharepoint联机API访问选项 - Missing Azure USGov Sharepoint Online API Access option in App Registration
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM