oauth授权_code流量未经授权,除非通过邮递员完成



我正在尝试获取使用Microsoft Identity平台的应用程序。

通过Postman发送OAuth请求似乎有效,但是当我尝试授权授权授予类型时,尽管获得了访问令牌,但我要访问的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

这使我在Querystring中使用代码重定向我的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 Console中创建了相同的邮政请求(尽管使用了不同的代码,但是它必须获得一个新代码,因为我已经兑换了另一个代码,并且知道它知道没关系吗?(但是它返回的访问令牌有效:

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

(忽略这是错误的事实 - 用户没有我要查询的应用程序的许可证,但这还可以(

我在做什么根本上做错了吗?从我所看到的,我正在正确关注Oauth流。

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

当身份平台生成重定向/回调时,它仅在执行get时似乎包含querystring元素,或者在执行帖子时的表单元素。

您可以在下面看到:

<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>

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

最新更新