简体   繁体   English

AWS API 网关令牌测试响应代码:401

[英]AWS API Gateway token test Response Code: 401

Thanks if anyone can help me.谢谢如果有人可以帮助我。 I am building a cognito user pool + API gateway solution in AWS.我正在 AWS 中构建一个认知用户池 + API 网关解决方案。 Now the configuration is done but the token is not working.现在配置已完成,但令牌不起作用。 Here is how I tested,这是我的测试方法,

I used API endpoint我使用了 API 端点

https://mydomain/login?response_type=token&client_id=5gjg8956um7bf2h5c3fuav1o46&redirect_uri=https://www.example.com

to get a token, here is the result.要获得令牌,结果如下。

https://www.example.com/#id_token=eyJraWQiOiJiTTcrSVlMUHBHVTBQK3FnTmkrMWxSeGFyNjRrb3hxYUluemptZllMTmZ3PSIsImFsZyI6IlJTMjU2In0.eyJhdF9oYXNoIjoiMmJXdHU5STVMQUJkQWFTSmM5S3ZtQSIsInN1YiI6ImYwNDk3NjgwLWJlNDEtNGNlZC1iMzQ4LWQ0NTg3M2Q5OTg2MSIsImF1ZCI6IjVnamc4OTU2dW03YmYyaDVjM2Z1YXYxbzQ2IiwiZW1haWxfdmVyaWZpZWQiOnRydWUsImV2ZW50X2lkIjoiODIyMWI3MjQtMzI5ZS00ZGJjLThjZjktZjhlMjY5NWFiNjkzIiwidG9rZW5fdXNlIjoiaWQiLCJhdXRoX3RpbWUiOjE1OTM5MDg1MjEsImlzcyI6Imh0dHBzOlwvXC9jb2duaXRvLWlkcC5hcC1zb3V0aGVhc3QtMi5hbWF6b25hd3MuY29tXC9hcC1zb3V0aGVhc3QtMl9Qa1ROR1RPNzIiLCJjb2duaXRvOnVzZXJuYW1lIjoiZjA0OTc2ODAtYmU0MS00Y2VkLWIzNDgtZDQ1ODczZDk5ODYxIiwiZXhwIjoxNTkzOTEyMTIxLCJpYXQiOjE1OTM5MDg1MjEsImVtYWlsIjoicWlhby5saUBob2xtZXNnbGVuLmVkdS5hdSJ9.lkTA49l_EQpWnhiLnKdbBR1anA0H4psFwEEBJuWgwQ6Iwg_GVZgvl3Sf0_p8OF-_vgRvcGbg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6ZyDOXhRaVQAmQSnagVr0_jPhH9Bw4_AS_4jNy4t27yDufpOnEgNWQW1sy96zpuaLFHJYAQblaJCxt_qbf_KETRDCil8ap63XUbAElaCvnSRrIGCcXmVOPChUMDSHVDu4CoMm9cgRQvj-kWFKP96YEO62tFa4_gZk1CICvjFEi7VCH0tvN9JVe8baSHm2GL1jaTeoUeE0jmGPGxGc-7fDBY37JjPbnPiHDZlm3D8eGE1AhO5qI3rng&expires_in=3600&token_type=Bearer

I verified the token on https://jwt.io/ and it is decodable.我验证了https://jwt.io/上的令牌,它是可解码的。 However, when trying to test the token in test tool in API gateway Authorizer, I got a 401 error.但是,当尝试在 API 网关授权器中的测试工具中测试令牌时,出现 401 错误。

Also, I tried to post the request in Postman as well and the result is also 401, with the following result.另外,我也尝试在 Postman 中发布请求,结果也是 401,结果如下。

{
"message": "Unauthorized"
}

My take is that if I can get a token through the endpoint, the token must be correct, right?我的看法是,如果我可以通过端点获取令牌,那么令牌一定是正确的,对吧? How can I troubleshoot?如何进行故障排除? thanks谢谢

Now I used the "wild rydes" app to sign in for a token, and the token will pass the Authorizer test in API gateway, also Postman API call is working.现在我使用“wild rydes”应用程序登录了一个令牌,该令牌将通过 API 网关中的 Authorizer 测试,Postman API 调用正在工作。

Still, the token generated by "Hosted UI" in the Cognito does not work, as in the original question.尽管如此,Cognito 中“托管 UI”生成的令牌仍然不起作用,就像在原始问题中一样。

The Cognito authorizer on your API Gateway will accept either the ID token or Access token, depending if you specified an OAUTH scope to the API Gateway method when adding the authorization. The Cognito authorizer on your API Gateway will accept either the ID token or Access token, depending if you specified an OAUTH scope to the API Gateway method when adding the authorization.

The Authorizer test on API Gateway will only accept the ID token. API 网关上的 Authorizer 测试将只接受 ID 令牌。 So I would suggest checking that you are getting a token from the correct Cognito UserPool that matches your API Gateway Cognito authorizer and then check your API Gateway method to see if you specified an OAUTH scope. So I would suggest checking that you are getting a token from the correct Cognito UserPool that matches your API Gateway Cognito authorizer and then check your API Gateway method to see if you specified an OAUTH scope. If specified a scope, this scope will need to be in the Access token sent to API Gateway.如果指定了 scope,则此 scope 将需要在发送到 API 网关的访问令牌中。 If no scope specified, send the ID token.如果未指定 scope,则发送 ID 令牌。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM