Nodemailer / Gmail - What is an update token and how to get it?

I am trying to create a simple contact form in a node application using nodemailer . I want all messages to be sent from the gmail account created for this purpose to my mail.

on the client side, all I do is get the name / mail / message of the client and send it to the server. It works fine locally, but doesn't work on deployment (on heroku btw).

After a quick search, it seems to me that I should create ClientId and ClientSecret from the Google Developers Console, which I did, but when it comes to creating an “update token”, I completely lost it.

  var smtpTransport = nodemailer.createTransport("SMTP",{ service:"Gmail", auth:{ XOAuth2: { user:"myaccount@gmail.com", clientId:"", clientSecret:"", refreshToken:"" } } }); 

I'm confused . What is an update token and how to get it?

+24
email gmail token nodemailer
Jun 07 '14 at 2:54
source share
2 answers

Notes on this answer by:

So, I finally managed to figure it out. I am surprised that I could not find more resources for this, so for those who should use Gmail with Nodemailer

I found the answer here: http://masashi-k.blogspot.fr/2013/06/sending-mail-with-gmail-using-xoauth2.html

Try creating a new user if you already have one and everything is not working fine. That was for me.

I hope this will be useful to someone

Greetings




Question 1: What is an update token?

From the documentation found here :

The update current gives your application constant access to the Google APIs until the user logs into your application.

(...)

Questions:

  • Be sure to keep the update token safe and permanent, because you can only get the update token the first time you execute a code exchange thread.

  • Are there restrictions on the number of update tokens that are issued - one limit for a client / user combination, and another for each user for all clients. If your application requests too many update tokens, it can work within these limits, in which case the old update tokens stop working.

See also Offline Access and Using Update Token .




Question 2: How do I get it?

Step 1: Get OAuth 2.0 Credentials in the Google Developers Console

As indicated here , you must:

  • Go to the Google Developer Console .
  • Select a project or create a new one.
  • In the left sidebar, expand the API and log in . Then click on the API . Click the Enabled APIs link in the API section to see a list of all supported APIs. Make sure the "Gmail API" is included in the list of allowed APIs. If you haven’t enabled it, select the Gmail API from the list of APIs (according to the Google Apps APIs), and then select Enable APIs for APIs.
  • In the left sidebar, select Credentials .
  • If you have not already done so, create your OAuth 2.0 project credentials by clicking Create a new client ID and providing the information needed to create the credentials.

Image from the blog post linked above

  1. Find the Client ID and Client table in the table associated with each of your credentials.

Image from the blogpost linked above




PAY SPECIAL ATTENTION by specifying https://developers.google.com/oauthplayground as a Redirect URI when you create a new user in the console. Otherwise, you will receive an error message.




Step 2: Get Update Token on Google OAuth2.0 Playground

  • Go to the Google Oauth2.0 Playground .
  • Click the Gear button in the upper right corner. Set the Client ID and Client Privacy obtained from the Google Developers Console , and select Token Access as the Authorization Header with the media prefix . Close this configuration.

Image from the blogpost above

  1. Set up areas. Use https://mail.google.com/ as you need, Nodemailer . Then click the Authorize API button.

enter image description here

  1. After OAuth2.0 authorization, exchange authorization code for tokens and voilá! your update token is ready to use

Image from the blogpost specified above

+63
Jun 09 '14 at 15:29
source share

For those looking for a working example / code snippet, follow the Radioreve Answer until you can get the access token and update the token. (Basically, go to the playground, make sure it requests access to send mail and mail.google.com, gives permission, an exchange authorization code for tokens)

Please note that the expires time expires was new Date().getTime() + 2000 , which was close to the seconds indicated on the playing court. I'm not sure if I should enter the access token and the expiration time exactly, as it seems to update the token automatically.

Use this sample code written in ECMAScript 6:

  const user_name = 'something@gmail.com'; const refresh_token = ''; const access_token = ''; const client_id = ''; const client_secret = ''; const email_to = 'receiver@gmail.com'; const nodemailer = require('nodemailer'); let transporter = nodemailer .createTransport({ service: 'Gmail', auth: { type: 'OAuth2', clientId: client_id, clientSecret: client_secret } }); transporter.on('token', token => { console.log('A new access token was generated'); console.log('User: %s', token.user); console.log('Access Token: %s', token.accessToken); console.log('Expires: %s', new Date(token.expires)); }); // setup e-mail data with unicode symbols let mailOptions = { from : user_name, // sender address to : email_to, // list of receivers subject : 'Hello ✔', // Subject line text : 'Hello world ?', // plaintext body html : '<b>Hello world ?</b>', // html body auth : { user : user_name, refreshToken : refresh_token, accessToken : access_token, expires : 1494388182480 } }; // send mail with defined transport object transporter.sendMail(mailOptions, function (error, info) { if (error) { return console.log(error); } console.log('Message sent: ' + info.response); }); 
+4
May 11 '17 at 7:12
source share



All Articles