preslavsh
preslavsh

Reputation: 41

Nodemailer XOauth2 [Error: unauthorized_client] when trying to get new access token

I am sending emails through Gmail succefully using nodemailer with xoauth2, but when the time comes to get new access token i receive [Error: unauthorized_client].

My code:

var express = require('express');
var nodemailer = require("nodemailer");
var xoauth2 = require('xoauth2');
app = express();

var generator = xoauth2.createXOAuth2Generator({
    user: {userEmail},
    clientId: {clientId},
    clientSecret: {clientSecret}
    refreshToken: {refreshToken},
    accessToken: {accessToken}
});

var smtpTransport = nodemailer.createTransport({
    service: "gmail",
    auth: {
        xoauth2: generator
    }
});

var mailOptions = {mailOptions} 

app.get('/send',
    function (req, res) {
        generator.generateToken(function (err, token, access) {
            console.log(err, token, access);
            smtpTransport.sendMail(mailOptions, function (error, response) {
                if (error) {
                    console.log(error);
                    res.sendStatus(404);
                } else {
                    console.log(response);
                    res.sendStatus(200);
                }
                smtpTransport.close();
            });
        });
    });

app.listen(5000);

I am invoking generateToken just for testing purposes. But in normal use is not responding also.

I obtained here Google Developers clientId and clientSecret, where i also added https://developers.google.com/oauthplayground/ to Authorized redirect URIs. After that I went developers playground (the same as the redirect uri), I selected Gmail API with scope mail.google.com, then I exchanged the "Authorization code" for refresh and access tokens and I checked "Auto-refresh the token before it expires". After the timeout has finished (the 3600 seconds) I am not able to obtain new accessToken using xoauth2.

Upvotes: 2

Views: 3475

Answers (2)

Dmitry Grinko
Dmitry Grinko

Reputation: 15204

Perhaps you were using

https://developers.google.com/oauthplayground/

instead of

https://developers.google.com/oauthplayground 

Upvotes: 1

preslavsh
preslavsh

Reputation: 41

Can be marked as closed. I have not found the reason for that problem. I just deleted the project in Google Developers Console and create new one. In new project i followed the same steps desribed above I and succesfully obtained an access token.

Upvotes: 1

Related Questions