1

当试图在Microsoft图表中列出“我”的目录项时,我收到400错误请求,并显示以下错误消息:“缺少必要的用户声明。”Microsoft Graph无法列出目录文件

重现步骤:

  1. 通过Application Registration Tool创建应用程序,授予权限Files.Read
  2. 使用创建客户端ID和客户端密钥来获得令牌(以下其guide)卷曲:

    curl -X POST -H "Cache-Control: no-cache" -H "Content-Type: application/x-www-form-urlencoded" -d 'grant_type=client_credentials&client_id=<client-id>&client_secret=<client-secret>=&resource=https://graph.microsoft.com' "https://login.microsoftonline.com/<my-tenant-id>/oauth2/token"

  3. 使用创建的<Access-Token>来制作一个要求列出根目录像(卷曲):

    curl -X GET -H "Authorization: Bearer <Access-Token>" -H "Cache-Control: no-cache" "https://graph.microsoft.com/v1.0/me/drive/root/children"

获取响应:

400 Bad Request 
{ 
    "error": { 
    "code": "BadRequest", 
    "message": "Missing necessary user claims.", 
    "innerError": { 
     "request-id": "36c384f4-1810-4d96-ad69-d69a67d11ece", 
     "date": "2016-05-31T14:39:05" 
    } 
    } 
} 

任何帮助,将不胜感激

回答

2

在你的榜样,你只认证该应用程序(称为“仅应用程序身份验证”,或者,参考OAuth 2.0流程,称为“客户端证书授予”流程)。但是,您的请求特别提及用户(.../me/...),即您。

对用户进行身份验证和授权的最常见(也是最完整的)方式是调用授权码授予流程,以获取应用程序和用户的访问令牌。在此流程结束时获取的访问令牌将包含有关登录用户的声明,并允许Microsoft Graph知道您指的是“我”。从the docs

To get your app authorized, you must get the user authenticated first. You do this by redirecting the user to the Azure Active Directory (Azure AD) authorization endpoint, along with your app information, to sign in to their Office 365 account. Once the user is signed in, and consents to the permissions requested by your app (if the user has not done so already), your app will receive an authorization code required to acquire an OAuth access token.

一些更多的阅读: