【问题标题】:Microsoft Graph API, password grant optionMicrosoft Graph API,密码授予选项
【发布时间】:2019-06-07 02:21:30
【问题描述】:

this主题中,作者解释了如何根据用户登录名和密码获取访问令牌,跳过授权对话框。

我可以点击端点并获取访问令牌,然后刷新令牌:

GET https://login.microsoftonline.com/348a...6e4/oauth2/token?grant_type=password&\
client_id=6d5f...64f&client_secret=g...f2&username=user@Domain.com&\
password=P@ssW0RD&resource=https://graph.microsoft.com/&\
scope=user.read%20calendars.read%20MailboxSettings.Read%20Mail.read%20Mail.Send

//RESPONSE
{
    "token_type": "Bearer",
    "scope": "User.Read",
    "expires_in": "3600",
    "ext_expires_in": "3600",
    "expires_on": "1559602338",
    "not_before": "1559598438",
    "resource": "https://graph.microsoft.com/",
    "access_token": "eyJ0eXAiOiJKV...KJmNhyz5bvhzCWiFA",
    "refresh_token": "AQABAAA...VkgAA",
    "id_token": "eyJ0...xLjAifQ."
}

但是当我尝试获取用户的电子邮件时,我收到了一个错误:

GET https://graph.microsoft.com/v1.0/me/messages?\
$select=sender,subject,ReceivedDateTime&\
$filter=ReceivedDateTime%20ge%202019-05-01&$top=20

RESPONSE

{
    "error": {
        "code": "ErrorAccessDenied",
        "message": "Access is denied. Check credentials and try again.",
        "innerError": {
            "request-id": "e35f...94",
            "date": "2019-06-03T21:39:40"
        }
    }
}

这种身份验证方法对 Microsoft Graph API 仍然有效吗?如果没有,是否有其他方法可以仅使用登录名和密码获得用户授权,无需任何用户交互,只需获取他的登录名和密码?

【问题讨论】:

    标签: microsoft-graph-api


    【解决方案1】:

    您不能使用 v1 端点动态指定 scope。仅较新的 v2 端点支持该模型(请参阅文档中的 Scopes, not Resources)。

    为了访问用户的邮箱,您需要将Mail.Read 添加到您在 Azure 门户中的应用程序注册。

    请记住,您需要管理员同意租户中所有用户的应用程序,或者让每个用户至少以交互方式登录一次,以便他们自己同意。

    如果我没有指出使用密码授权几乎总是是个坏主意,我也会失职。它从根本上不如其他所有 OAuth 授权安全。

    【讨论】:

    • 我们在管理员同意的情况下解决了访问被拒绝访问的问题,现在它正在工作。我们知道这是最好和更安全的方式,但它符合我们在这个项目中的需要。感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多