使用 Auth0 将 authorization_code 换成 access_token 和 id_token

Exchange authorization_code for access_token and id_token not working using Auth0

我正在使用 Auth0 短信无密码登录,我可以正确登录,并且我被正确重定向到我指定的回调 url:http://localhost:8000/authenticated?code=AUTHORIZATION_CODE。我一直在关注 this 教程,但是当我进入第 4 步和第 5 步将 authorization_code 换成 access_token 和 id_token 时,我收到此错误消息:{"error":"access_denied","error_description":"Unauthorized"}.

这就是我通过 POST:

将代码发送到 Auth0 服务器的方式
var code = request.query.code;
var url = `https://${process.env.AUTH0_CLIENT_DOMAIN}/oauth/token?client_id=${process.env.AUTH0_CLIENT_ID}&redirect_uri=http://localhost:8000/authenticated&client_secret=${process.env.AUTH0_CLIENT_SECRET}&code=${code}&grant_type=authorization_code`;

Wreck.post(url, (err, res, payload) => {
  console.log(payload.toString());
});

我的查询字符串中是否缺少某些内容?或者在发送此 post 请求之前我需要做些什么?

我的问题在 auth0 存储库的一个问题中得到了回答:https://github.com/auth0/auth0.js/issues/234

不过我把答案转贴在这里:

Post 有效负载,而不是在查询字符串中将其作为参数发送:

var code = request.query.code;
var url = `https://${process.env.AUTH0_CLIENT_DOMAIN}/oauth/token`;
var body = { 
  client_id:process.env.AUTH0_CLIENT_ID,
  redirect_uri:'http://localhost:8000/authenticated',
  client_secret:process.env.AUTH0_CLIENT_SECRET,
  code:code,
  grant_type:'authorization_code'
};

Wreck.post(url, {payload:body}, (err, res, payload) => {
    console.log(payload.toString());
});