无法使用 googleapis 从 developers.google.com 获取访问令牌

Not able get access token from developers.google.com using googleapis

提问人:bagewadi-akram 提问时间:3/3/2023 最后编辑:bagewadi-akram 更新时间:3/10/2023 访问量:153

问:

我尝试从 https://developers.google.com/oauthplayground 获取 oauth2 访问令牌,但收到此错误GaxiosError: invalid_grant at Gaxios._request (/home/epit/Desktop/app/server/node_modules/gaxios/build/src/gaxios.js:129:23) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async OAuth2Client.refreshTokenNoCache (/home/epit/Desktop/app/server/node_modules/google-auth-library/build/src/auth/oauth2client.js:172:21) at async OAuth2Client.refreshAccessTokenAsync (/home/epit/Desktop/app/server/node_modules/google-auth-library/build/src/auth/oauth2client.js:196:19) at async OAuth2Client.getAccessTokenAsync (/home/epit/Desktop/app/server/node_modules/google-auth-library/build/src/auth/oauth2client.js:216:23) at async sendMail (/home/epit/Desktop/app/server/mail/sendmails.js:22:25) { response: { config: { method: 'POST', url: 'https://oauth2.googleapis.com/token', data: 'refresh_token=4%2F0AWtgzh4QgLUVbkqdum53tNC6LLDzehz6FewksokLu9cuRFlKcXM3xl070WQNQto65Txttg&client_id=741493752633-ema2rkmv1f3s9ehkkq0mc8ii4erburaj.apps.googleusercontent.com&client_secret=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&grant_type=refresh_token', headers: [Object], paramsSerializer: [Function: paramsSerializer], body: 'refresh_token=4%2F0AWtgzh4QgLUVbkqdum53tNC6LLDzehz6FewksokLu9cuRFlKcXM3xl070WQNQto65Txttg&client_id=741493752633-ema2rkmv1f3s9ehkkq0mc8ii4erburaj.apps.googleusercontent.com&client_secret=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&grant_type=refresh_token', validateStatus: [Function: validateStatus], responseType: 'json' }, data: { error: 'invalid_grant', error_description: 'Bad Request' }, headers: { 'alt-svc': 'h3=":443"; ma=2592000,h3-29=":443"; ma=2592000', 'cache-control': 'no-cache, no-store, max-age=0, must-revalidate', connection: 'close', 'content-encoding': 'gzip', 'content-type': 'application/json; charset=utf-8', date: 'Fri, 03 Mar 2023 14:26:27 GMT', expires: 'Mon, 01 Jan 1990 00:00:00 GMT', pragma: 'no-cache', server: 'scaffolding on HTTPServer2', 'transfer-encoding': 'chunked', vary: 'Origin, X-Origin, Referer', 'x-content-type-options': 'nosniff', 'x-frame-options': 'SAMEORIGIN', 'x-xss-protection': '0' }, status: 400, statusText: 'Bad Request', request: { responseURL: 'https://oauth2.googleapis.com/token' } }, config: { method: 'POST', url: 'https://oauth2.googleapis.com/token', data: 'refresh_token=4%2F0AWtgzh4QgLUVbkqdum53tNC6LLDzehz6FewksokLu9cuRFlKcXM3xl070WQNQto65Txttg&client_id=741493752633-ema2rkmv1f3s9ehkkq0mc8ii4erburaj.apps.googleusercontent.com&client_secret=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&grant_type=refresh_token', headers: { 'Content-Type': 'application/x-www-form-urlencoded', 'User-Agent': 'google-api-nodejs-client/6.1.6', 'x-goog-api-client': 'gl-node/18.7.0 auth/6.1.6', Accept: 'application/json' }, paramsSerializer: [Function: paramsSerializer], body: 'refresh_token=4%2F0AWtgzh4QgLUVbkqdum53tNC6LLDzehz6FewksokLu9cuRFlKcXM3xl070WQNQto65Txttg&client_id=741493752633-ema2rkmv1f3s9ehkkq0mc8ii4erburaj.apps.googleusercontent.com&client_secret=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&grant_type=refresh_token', validateStatus: [Function: validateStatus], responseType: 'json' }, code: '400' }

这些是进口

const { google } = require("googleapis");
const { OAuth2 } = google.auth;

这是我的代码

const oAuth2Client = new google.auth.OAuth2(
 CLIENT_ID,
 CLIENT_SECRET,
 REDIRECT_URI
);
oAuth2Client.setCredentials({ refresh_token: REFRESH_TOKEN });

const accessToken = await oAuth2Client.getAccessToken();

IAM 使用“googleapis”的 npm 包:“^112.0.0”,

进一步,我将在这里使用accessToken

 const transport = nodemailer.createTransport({
      service: "gmail",
      auth: {
        type: "OAuth2",
        user: "[email protected]",
        clientId: CLIENT_ID,
        clientSecret: CLIENT_SECRET,
        refreshToken: REFRESH_TOKEN,
        accessToken: accessToken,
       },
    });

节点 .js npm apis-explorer 谷歌开发者工具

评论

0赞 Pavel Sapehin 3/4/2023
请使用您所做的代码添加步骤,并提及不适合您的步骤。

答: 暂无答案