带有 MS Graph API 的 Azure AD,无法授予我的应用程序 Mail.Read

Azure AD with MS Graph API, can't grant my app Mail.Read

正在尝试使用 Microsoft Graph API 阅读 Office 365 邮件。我可以通过 Azure AD(不是 V2)进行身份验证并获得令牌,但我获得的唯一权限是 User.Read。我要 Mail.Read.

我在 Azure 门户中的应用程序注册包括 Mail.Read 权限:API "Microsoft Graph",权限 "Read user mail"。在 /authorize 请求中,我指定了 resourcescope,后者是 https://graph.windows.net/mail.read。然而,当令牌 JSON 返回时,它有 "scope":"User.Read"。并且请求 https://graph.microsoft.com/v1.0/me/messages returns 错误 403,而 https://graph.microsoft.com/v1.0/me/ 按预期工作。

我错过了什么?

编辑:初始 OAuth URI 是:

https://login.microsoftonline.com/common/oauth2/authorize?response_type=code&redirect_uri=https%3a%2f%2fredacted.com%2fgoblin%2fback.aspx&client_id=3b595e3f-aecf-4a8c-8ec3-309810d75783&response_mode=query&scope=https%3a%2f%2fgraph.windows.net%2fmail.read&resource=https%3a%2f%2fgraph.windows.net%2f

JSON 是:

{
    "token_type": "Bearer",
    "scope": "User.Read",
    "expires_in": "3599",
    "ext_expires_in": "0",
    "expires_on": "1494364932",
    "not_before": "1494361032",
    "resource": "https://graph.microsoft.com/",
    "access_token": "redacted",
    "refresh_token": "redacted",
    "id_token": "redacted"
}

看来您只需要再次触发同意即可。

如果您有一个应用程序,并且更新了该应用程序所需的权限,则需要征得用户同意您所需的新权限。

在您的代码中,您应该进行逻辑检查:

  • if 缺少范围,then 再次提示同意

您可以通过将以下查询字符串直接添加到登录名来提示同意 url:&prompt=consent

否则,您可以在 ADAL 中使用 PromptBehavior 参数。