Nodemailer/Gmail - What exactly is a refresh token and how do I get one?

Notes by this answer original's author:

So, I finally managed to figure it out. I'm surprised I couldn't find more ressources about that so for those who need to 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 had one and things ain't working fine. It was the case for me.

I hope this will be useful to someone,

Cheers


Question 1: What exactly is a refresh token?

From documentation found here:

A refresh token provides your app continuous access to Google APIs while the user is not logged into your application.

(...)

Considerations:

  • Be sure to store the refresh token safely and permanently, because you can only obtain a refresh token the first time that you perform the code exchange flow.

  • There are limits on the number of refresh token that are issued—one limit per client/user combination, and another per user across all clients. If your application requests too many refresh tokens, it may run into these limits, in which case older refresh tokens stop working.

See also Offline Access and Using a refresh token.


Question 2: How do I get one?

Step 1: Obtain OAuth 2.0 credentials at Google Developers Console

As stated here, you should:

  1. Go to the Google Developers Console.
  2. Select a project, or create a new one.
  3. In the sidebar on the left, expand APIs & auth. Next, click APIs. Select the Enabled APIs link in the API section to see a list of all your enabled APIs. Make sure that the "Gmail API" is on the list of enabled APIs. If you have not enabled it, select the Gmail API from the list of APIs (under Google Apps APIs), then select the Enable API button for the API.
  4. In the sidebar on the left, select Credentials.
  5. If you haven't done so already, create your project's OAuth 2.0 credentials by clicking Create new Client ID, and providing the information needed to create the credentials.

Image from the blog post linked above

  1. Look for the Client ID and Client secret in the table associated with each of your credentials.

Image from the blogpost linked above


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


Step 2: Obtain the refresh token at Google OAuth2.0 Playground

  1. Go to the Google Oauth2.0 Playground.
  2. Click the Gear Button on the right-top. Set your Client ID and Client Secret obtained from the Google Developers Console, and select Access token location as Authorization header w/ Bearer prefix. Close this configuration overlay.

Image from the blogpost above

  1. Set up the scopes. Use https://mail.google.com/ as it's the one need by nodemailer. Then click the Authorize APIs button.

enter image description here

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

Image from the blogpost specified above


For those who have been looking around for a working example/code snippet, follow Radioreve's Answer until you are able to get the access token and refresh token. (Basically, go to the playground, make sure it asks for access for sending mail and mail.google.com, give permission, exchange authorization code for tokens)

Note that the expires time I entered was new Date().getTime() + 2000 which was close to the expiration seconds seen on the playground. I am not sure if I had to enter access token and expiration time accurately as it seems to be refreshing the token automatically.

Use this sample code written in ECMAScript 6:

    const user_name     = '[email protected]';
    const refresh_token = '';
    const access_token  = '';
    const client_id     = '';
    const client_secret = '';

    const email_to = '[email protected]';

    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);
    });