除非通过邮递员完成,否则OAuth授权代码流未经授权

时间:2019-05-13 20:42:09

标签: oauth-2.0 postman

我正在尝试使应用程序能够与Microsoft Identity平台一起使用。

通过Postman发送OAuth请求似乎可以正常工作,但是当我自己尝试使用authorizaton_code授予类型时,尽管取回了访问令牌,但我尝试访问的API始终会给我带来未经授权的错误。

我正在向POST发送请求:

https://login.windows.net/<tenant_id>/oauth2/authorize?resource=<resource_uri>

带有身体数据:

grant_type=authorization_code&
client_id=<client_id>&
redirect_uri=<redirect_uri>&
response_type=code

这使我使用查询字符串中的代码重定向到我的URI

然后我将POST发送到:

https://login.windows.net/<tenant_id>/oauth2/token?resource=<resource_uri>

具有以下内容:

grant_type=authorization_code&
client_id=<client_id>&
redirect_uri=<redirect_uri>&
code=<the_code_from_the_redirect>&
client_secret=<client_secret>

这给了我一个访问令牌:

{
    "token_type": "Bearer",
    "expires_in": "3599",
    "ext_expires_in": "3599",
    "expires_on": "1557783183",
    "not_before": "1557779283",
    "resource": "00000002-0000-0000-c000-000000000000",
    "access_token": "<access_token_here>",
    "refresh_token": "<refresh_token>",
    "id_token": "<id_token>"
}

但是在调用资源时此令牌不起作用:

{
    "error": {
        "code": "Unauthorized",
        "message": "The credentials provided are incorrect"
    }
}

使用Postman中的Get New Access Token功能执行相同的操作似乎会在Postman控制台中创建相同的发布请求(尽管使用了不同的代码,但是它必须获得新的代码,因为我已经兑换了另一个代码一个并且对此一无所知吗?)但是它返回的访问令牌有效:

{
    "error": {
        "code": "NoLicense",
        "message": "User has no license"
    }
}

(忽略这是一个错误的事实-用户没有我要查询的应用程序的许可证,但是没关系)

我在根本上做错了吗?据我所知,我正确地遵循了OAuth流程。

1 个答案:

答案 0 :(得分:0)

弄清楚了-这是因为我将资源作为查询字符串的一部分而不是表单的一部分传递。

当身份平台生成重定向/回调时,它似乎仅在执行GET时包含querystring元素,而在执行POST时仅包含form元素。

您可以在下面看到它:

<html>
    <head>
        <title>Continue</title>
    </head>
    <body>
        <form method="POST" name="hiddenform" action="https://login.microsoftonline.com/<tenant_id>/oauth2/authorize">
            <input type="hidden" name="grant_type" value="authorization_code" />
            <input type="hidden" name="client_id" value="<client_id>" />
            <input type="hidden" name="redirect_uri" value="https://businesscentral.dynamics.com" />
            <input type="hidden" name="response_type" value="code" />
            <input type="hidden" name="scope" value="" />
            <noscript>
                <p>Script is disabled. Click Submit to continue</p>
                <input type="submit" value="Submit" />
            </noscript>
        </form>
        <script language="javascript">window.setTimeout('document.forms[0].submit()', 0);</script>
    </body>
</html>

resource添加到表单数据而不是URL后,我得到了一个稍微不同的重定向:

<html>
    <head>
        <title>Continue</title>
    </head>
    <body>
        <form method="POST" name="hiddenform" action="https://login.microsoftonline.com/<tenant_id>/oauth2/authorize">
            <input type="hidden" name="grant_type" value="authorization_code" />
            <input type="hidden" name="client_id" value="<client_id>" />
            <input type="hidden" name="redirect_uri" value="https://businesscentral.dynamics.com" />
            <input type="hidden" name="response_type" value="code" />
            <input type="hidden" name="scope" value="" />
            <input type="hidden" name="resource" value="<resource_id>" />
            <noscript>
                <p>Script is disabled. Click Submit to continue</p>
                <input type="submit" value="Submit" />
            </noscript>
        </form>
        <script language="javascript">window.setTimeout('document.forms[0].submit()', 0);</script>
    </body>
</html>

这为我生成了正确的访问令牌!