在emailTransporter.js我的代码如下。
const nodemailer = require('nodemailer');
const { google } = require("googleapis");
const path = require('path');
const OAuth2 = google.auth.OAuth2;
require('dotenv').config({path: path.join(__dirname, '.env')});
const oauth2Client = new OAuth2(
process.env.CLIENT_ID, // ClientID
process.env.CLIENT_SECRET, // Client Secret
"https://developers.google.com/oauthplayground" // Redirect URL
);
oauth2Client.setCredentials({
refresh_token: process.env.REFRESH_TOKEN
});
const accessToken = oauth2Client.getAccessToken();
let transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 465,
secure: true,
auth: {
type: "OAuth2",
user: process.env.USER_EMAIL,
clientId: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
refreshToken: process.env.REFRESH_TOKEN,
accessToken: accessToken
},
tls: {
rejectUnauthorized: false
}
});
module.exports = transporter;
在auth.js中,我使用emailTransporter.js中的emailTransporter导出来发送一些电子邮件选项。但是node给我的错误是这样的:
D:\nodejs\node_modules\gaxios\build\src\gaxios. js:129 throw new common_1.GaxiosError(Request failed with status code ${translatedResponse.status}
,opts,translatedResponse);^
GaxiosError:unauthorized_client在Gaxios。_request(D:\nodejs\node_modules\gaxios\build\src\gaxios。js:一百二十九:23)在processTicksAndRejections(节点:internal/process/task_queues:九十六:5)at async OAuth2Client.refreshTokenNoCache(D:\nodejs\node_modules\google-auth-library\build\src\auth\oauth2client.js:一百七十四:21)at async OAuth2Client.refreshAccessTokenAsync(D:\nodejs\node_modules\google-auth-library\build\src\auth\oauth2client.js:一百九十八:19)at async OAuth2Client.getAccessTokenAsync(D:\nodejs\node_modules\google-auth-library\build\src\auth\oauth2client.js:二百二十七:23){响应:{配置:{方法:'POST',url:'https://oauth2.googleapis.com/token',data:'refresh_token = 1%2F%2F04PvTQnD4_www.example.com & client_secret = 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 & grant_type = refresh_token',header:86nCgYIARAAGAQSNwF-L9IrTdRMaV9mdIMhyEhFeJeKGSvqYnbgkCw-18aF7ZFmRgpWoWiQM3EXozwMiXzvfYwmAwc&client_id=543495663437-d59lg6u1a77o9a2uinf83idb0md991l9.apps.googleusercontent.com'application/x-www-form-urlencoded',' User-Agent':'google-api-nodejs-client/7.11.0',' x-goog-api-client':'gl-node/16.13.2 auth/7.11.0',接受:'application/json '},paramsSerializer:[函数:paramsSerializer],函数体:2019 - 01 - 22 00:00:00:00 Copyright © 2018 - 2019 www.d59lg6u1a.com版权所有77o9a2uinf83idb0md991l9.apps.googleusercontent.com&client_secret = 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 & grant_type = refresh_token',validateStatus:[Function:validateStatus],responseType:'json '},数据:{error:'unauthorized_client',error_description:'Unauthorized '},标头:{'alt-svc':'h3 =":443"; ma = 2592000,h3 - 29 =":443"; ma = 2592000,h3-Q050 =":443"; ma = 2592000,h3-Q046 =":443"; ma = 2592000,h3-Q043 =":443"; ma = 2592000,quic =":443"; ma = 2592000 v ="46,43"',' cache-control':'no-cache,no-store,max-age = 0,must-revalidate',连接:'close','content-encoding':'gzip','content-type':'application/json; charset = utf-8',日期:'Wed,19 Jan 2022 03:47:31 GMT',过期时间:'Mon,01 Jan 1990 00:00:00 GMT',pragma:'no-cache',服务器:'HTTPServer2上的脚手架','transfer-encoding':'chunked',vary:'Origin,X-Origin,Refererer',' x-content-type-options':'nosniff',' x-frame-options':'SAMEORIGIN',' x-xss-protection':“0”},状态:401,状态文本:“未经授权”,请求:{responseURL:'https://oauth2.googleapis.com/token '}},配置:{method:'POST',url:'https://oauth2.googleapis.com/token',data:'refresh_token = 1%2F%2F04PvTQnD4_www.example.com & client_secret = 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 & grant_type = refresh_token',header:{'Content-Type ':'application/x-www-form-urlencoded',' User-Agent':86nCgYIARAAGAQSNwF-L9IrTdRMaV9mdIMhyEhFeJeKGSvqYnbgkCw-18aF7ZFmRgpWoWiQM3EXozwMiXzvfYwmAwc&client_id=543495663437-d59lg6u1a77o9a2uinf83idb0md991l9.apps.googleusercontent.com'gl-node/16.13.2 auth/7.11.0',接受:'application/json '},paramsSerializer:[函数:paramsSerializer],函数体:2019 - 01 - 22 00:00:00:00 Copyright © 2018 - 2019 www.d59lg6u1a.com版权所有77o9a2uinf83idb0md991l9.apps.googleusercontent.com&client_secret = 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 & grant_type = refresh_token',validateStatus:[Function:validateStatus],responseType:'json '},代码:'401 '} 'refresh_token=1%2F%2F04PvTQnD4_86nCgYIARAAGAQSNwF-L9IrTdRMaV9mdIMhyEhFeJeKGSvqYnbgkCw-18aF7ZFmRgpWoWiQM3EXozwMiXzvfYwmAwc&client_id=543495663437-d59lg6u1a77o9a2uinf83idb0md991l9.apps.googleusercontent.com&client_secret=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&grant_type=refresh_token', validateStatus: [Function: validateStatus], responseType: 'json' }, code: '401' }
2条答案
按热度按时间acruukt91#
在https://developers.google.com/oauthplayground/中,我必须选择右上方的“使用您自己的凭据”选项。填写cliendId和clientSecret,它已经工作了。
mfuanj7w2#