AWS Cognito TOKEN Endpoint 给出 400 Bad Request 错误“unauthorized_client”

Mat*_*ner 6 amazon-web-services amazon-cognito

成功检索身份验证代码后,按照https://docs.aws.amazon.com/cognito/latest/developerguide/token-endpoint.html 中的文档进行操作。

据我所知,这正是请求的设置方式:

import request from 'request'

function fetchToken(code: any, clientId: string, clientSecret: string) {
  try {
    let tokenEndpoint = `https://example.auth.us-east-1.amazoncognito.com/oauth2/token`
    const clientIdEncoded = Buffer.from(`${clientId}:${clientSecret}`).toString('base64')

    request.post({
      url:tokenEndpoint,
      headers: {
        'Content-Type':'application/x-www-form-urlencoded',
        'Authorization':`Basic ${clientIdEncoded}`
      },
      form: {
        code,
        'grant_type':'authorization_code',
        'client_id':clientId,
        'redirect_uri':'http://localhost:3000'
      }},
      function(err,httpResponse,body){
        console.log(httpResponse.statusCode)
        //400
        console.log(httpResponse.statusMessage)
        //Bad Request
        if(err) {
          console.error(err)
        }

        console.log(body)
        //{"error":"unauthorized_client"}
      })
  } catch (error) {
    console.error(error)
  }
}
Run Code Online (Sandbox Code Playgroud)

为什么会得到unauthorized_client?有没有更简单的方法来调试这个?

编辑:使用相同的请求在 Postman 中对此进行了测试并得到相同的错误

标题 标题 身体 身体

小智 3

请检查 Cognito 用户池应用程序是否使用密钥。如果您使用密钥选项创建,则必须将其包含在Authorization请求的标头中。