尝试为jwt交换cognito代码时出现格式错误的请求

时间:2018-06-18 11:27:38

标签: javascript amazon-web-services

编辑:

我正在阅读错误的文档。在这个JSON世界中,我没有注意到请求是在发送表单数据。愚蠢的错误。

这也是错误的终点。

请求应该看起来像:

fetch(
    'https://api.amazon.com/auth/o2/token/',
    {
        method: 'POST',
        headers:{
            'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
        },
        body: 
            `?redirect_uri=${redirectUri}` +
            `&code=${clientCode}` +
            `&client_id=${clientId}` +
            `&client_secret=${clientSecret}` +
            '&grant_type=authorization_code'
    }
)

我正在尝试使用cognito的登录页面中的代码流交换获得的令牌,但请求的状态为400“格式错误的请求”。

我关注的文档是:https://developer.amazon.com/docs/login-with-amazon/authorization-code-grant.html#access-token-request

我正在使用浏览器发出以下请求:

   fetch(
        'https://api.amazon.com/auth/o2/token/' +
        `?redirect_uri=${encodeURIComponent(redirectUri)}` +
        `&code=${encodeURIComponent(clientCode)}` +
        `&client_id=${encodeURIComponent(clientId)}` +
        `&client_secret=${encodeURIComponent(clientSecret)}` +
        '&grant_type=authorization_code',
        {
            method: 'POST',
            headers:{
                'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
            }
        }
    )

这导致客户端发送以下请求: POST

https://api.amazon.com/auth/o2/token/?redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Floggedin&code=<code>&client_id=<client_id>o&client_secret=<client_secret>&grant_type=authorization_code

回应:

{
    "error_description": "Malformed request",
    "error":"invalid_request"
}

0 个答案:

没有答案
相关问题