Fwd: 使用升级 google identity web授权成功,换取令牌token失败

1 view
Skip to first unread message

Wenshan He

unread,
Mar 11, 2024, 5:03:51 AM3/11/24
to googl...@googlegroups.com


---------- 转发的邮件 ---------
发件人:Google Identity OAUTH2 <google-iden...@googlegroups.com>
日期:2024年3月11日星期一UTC+8 15:06:08
主题:使用升级 google identity web授权成功,换取令牌token失败
收件人: Google Identity OAUTH2 <google-iden...@googlegroups.com>


H1,

前提: web授权成功获取到code授权码

换取令牌过程:
   1. 方案一:
如图:
截屏2024-03-11 14.50.25.png
       我的代码实现如下:
const { google } = require('googleapis');

getToken() {
const { ctx } = this;
const { code } = ctx.request.body;
const { client_id, client_secret, redirect_uri, response_type, scope } = ctx.app.config.oauth2Google;
const oauth2Client = new google.auth.OAuth2(client_id, client_secret, redirect_uri);
let data = await oauth2Client.getToken(code);
console.log('data:', data);
}

  出现的错误信息:
config: {"method":"POST","url":"https://oauth2.googleapis.com/token","data":"< - See errorRedactor option in gaxios for configuration >.","headers":{"Content-Type":"application/x-www-form-urlencoded","User-Agent":"google-api-nodejs-client/9.6.3","x- goog-api-client":"gl-node/20.11.0"},"body":"< - See errorRedactor option in gaxios for configuration>.","responseType":"unknown" }
response: undefined
error: {"message":"request to https://oauth2.googleapis.com/token failed, reason: read ECONNRESET","type":"system","errno":"ECONNRESET","code":" ECONNRESET"}

  2.方案二:

getToken() {
const { ctx } = this;
const time = new Date().getTime();
const { code } = ctx.request.body;
const { client_id, client_secret, redirect_uri, response_type, scope } = ctx.app.config.oauth2Google;
console.log(code);
const data = await ctx.axios.post(
new URLSearchParams({
grant_type: 'authorization_code',
code,
client_id,
client_secret,
redirect_uri
})
);
console.log('data:', data);
}

出现的错误信息:
   请求的状态码是 400

小结:
   在以上的方案中以及线下postman测试都没有获取到token。
根据官方的文档指引也没有获取到。

诉求:
 1. 我如何正确的获取到令牌token
 2.  JS 或是 node 方式请求 google api 接口,如何正确的发起请求,参照 oauthplayground 的调试也是不无法实现,请给我具体用例。

谢谢。



Reply all
Reply to author
Forward
0 new messages