Skip to content

Instantly share code, notes, and snippets.

@jermsam
Last active March 22, 2019 12:54
Show Gist options
  • Save jermsam/64492326f7bbc0ab043f8653de5d2bf1 to your computer and use it in GitHub Desktop.
Save jermsam/64492326f7bbc0ab043f8653de5d2bf1 to your computer and use it in GitHub Desktop.
Authentication Bug With Gmail Api Oauth2.
error: Error: Mail command failed: 530-5.5.1 Authentication Required. Learn more at
530 5.5.1 https://support.google.com/mail/?p=WantAuthError m17sm5782308pff.170 - gsmtp
at SMTPConnection._formatError (C:\Users\JITPOMI\code\chat-app\chat-s\node_modules\feathers-mailer\node_modules\nodemailer\lib\smtp-connection\index.js:605:19)
at SMTPConnection._actionMAIL (C:\Users\JITPOMI\code\chat-app\chat-s\node_modules\feathers-mailer\node_modules\nodemailer\lib\smtp-connection\index.js:1370:34)
at SMTPConnection._responseActions.push.str (C:\Users\JITPOMI\code\chat-app\chat-s\node_modules\feathers-mailer\node_modules\nodemailer\lib\smtp-connection\index.js:857:18)
at SMTPConnection._processResponse (C:\Users\JITPOMI\code\chat-app\chat-s\node_modules\feathers-mailer\node_modules\nodemailer\lib\smtp-connection\index.js:764:20)
at SMTPConnection._onData (C:\Users\JITPOMI\code\chat-app\chat-s\node_modules\feathers-mailer\node_modules\nodemailer\lib\smtp-connection\index.js:570:14)
at TLSSocket._socket.on.chunk (C:\Users\JITPOMI\code\chat-app\chat-s\node_modules\feathers-mailer\node_modules\nodemailer\lib\smtp-connection\index.js:522:47)
at TLSSocket.emit (events.js:189:13)
at TLSSocket.EventEmitter.emit (domain.js:441:20)
at addChunk (_stream_readable.js:288:12)
at readableAddChunk (_stream_readable.js:269:11)
at TLSSocket.Readable.push (_stream_readable.js:224:10)
at TLSWrap.onStreamRead [as onread] (internal/stream_base_commons.js:145:17)
error: Unhandled Rejection at: Promise {"meta":[{},{"code":"EENVELOPE","response":"530-5.5.1 Authentication Required. Learn more at\n530 5.5.1 https://support.google.com/mail/?p=WantAuthError m17sm5782308pff.170 - gsmtp","responseCode":530,"command":"MAIL FROM"}]}
const Proto = require('uberproto');
const Mailer = require('nodemailer');
const debug = require('debug')('feathers-mailer');
class Service {
constructor (transport, defaults) {
debug('constructor', transport);
if (!transport) {
throw new Error('feathers-mailer: constructor `transport` must be provided');
}
this.transporter = Mailer.createTransport(transport, defaults);
}
extend (obj) {
return Proto.extend(obj, this);
}
create (body, params) {
debug('create', body, params);
// TODO maybe body should be text/html field
// and params is rest of options
// https://github.com/nodemailer/nodemailer#set-up-smtp says:
// If callback argument is not set then the method returns a Promise object.
return this.transporter.sendMail(body);
}
}
module.exports = function init (transport, defaults) {
return new Service(transport, defaults);
};
module.exports.Service = Service;
// Initializes the `mailer` service on path `/mailer`
// mailer is an email sending service
/* eslint-disable no-console */
const hooks = require('./mailer.hooks');
const Mailer = require('feathers-mailer');
const { google } = require('googleapis');
const OAuth2 = google.auth.OAuth2;
module.exports = async function(app) {
try {
const { user, clientId, clientSecret, refreshToken } = app.get('xoauth2');
/**
* Create a new OAuth2 client with the configured keys.
*/
const oauth2Client = new OAuth2(
clientId, // ClientID
clientSecret, // Client Secret
'https://developers.google.com/oauthplayground' // Redirect URL
);
/**
* Access tokens expire.
* This library will automatically use a refresh token to obtain a new access token if it is about to expire.
*/
oauth2Client.setCredentials({
refresh_token: refreshToken,
});
// const tokens = await oauth2Client.refreshAccessToken();
// const accessToken = tokens.credentials.access_token;
const {res} = await oauth2Client.getAccessToken();
const { access_token, expiry_date } = res.data;
const accessToken = access_token;
const expires=expiry_date;
console.log('access token: ',accessToken, ' expires: ',expires);
app.use(
'/mailer',
Mailer({
host: 'smtp.gmail.com',
port: 465,
secure: true,
service: 'Gmail',
auth: {
type: 'OAuth2',
user,
clientId,
clientSecret,
refreshToken,
accessToken,
expires
},
tls: {
rejectUnauthorized: false, // do away with sendmail error : self signed certificate
},
})
);
// Get our initialized service so that we can register hooks and filters
const service = app.service('mailer');
service.hooks(hooks);
} catch (err) {
console.log('Error: ', err.message);
}
};
@jermsam
Copy link
Author

jermsam commented Mar 21, 2019

Hi. I am implementing a emailer service with nodemailer and googleapi, making use of their gmailAPI and oauthplayground as authorised redirect url... but I am getting the bug in error.log above. Kindly help me fix it.

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment