Microsoft graph API:与附加的共享点和令牌一起使用时出现未经授权的错误

Microsoft graph API: unauthorized error when used with sharepoint and token attached

我正在开发 Sharepoint 应用程序。对于我的应用程序,我必须使用 Microsoft 图形端点从 Azure AD 获取我的详细信息。我已经使用 /token 端点和 client_credentials 授权类型生成了访问令牌。当我将响应中收到的令牌与图形 /users 端点一起使用时,出现未经授权的错误,如下图所示:

在 Azure 门户上,所有权限都授予了 Microsoft Graph API。是否缺少任何配置?我该如何解决这个未经授权的错误?

我认为您在令牌中缺少使用 "User" 端点的正确范围。 可能的范围是:

  • User.ReadBasic.All
  • User.Read
  • User.Read写入
  • User.Read.全部
  • User.ReadWrite.All

详情请看here

未使用v2.0端点进行测试,如果可以接受使用v1.0端点,您可以参考步骤。

1.Navigate 到门户中的 Azure Active Directory -> App registrations -> New application registration,更多详细信息请参阅此 link

2.Go到AD App -> Keys -> 为AD App生成key,复制key值。

3.Then 去 Required permissions -> Add -> select Microsoft GraphRead all users' full profilesAPPLICATION PERMISSIONS -> Save , 注意 然后不要忘记点击 Grant permissions 按钮。

4.I在postman中测试一下,具体body我们需要得到什么access_tokenclient_id是AD App的Application IDclient_secret是你复制的key vaulegrant_typeclient_credentialsresourcehttps://graph.microsoft.com/.

样本:

POST https://login.microsoftonline.com/<Tenant ID>/oauth2/token?api-version=1.0

5.Use access_token 调用 MS Graph API,在我的示例中,我调用 List users api,它工作正常。