Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

oauth gmail error: { [Error: unauthorized_client] code: 'EAUTH' } #564

Closed
connor11528 opened this issue Feb 13, 2016 · 8 comments
Closed

Comments

@connor11528
Copy link

I have login with google oauth set up. I pasted in the accessToken field from what got returned from login with google. I cannot get past this error { [Error: unauthorized_client] code: 'EAUTH' }.

var express = require('express'),
    nodemailer = require('nodemailer'),
    xoauth2 = require('xoauth2'),
    router = express.Router();

// create reusable transporter object using the default SMTP transport
var transporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
        xoauth2: xoauth2.createXOAuth2Generator({
            user: "connorleech@gmail.com",
            clientId: '925......4cui6indku1jgr7g7kpg6.apps.googleusercontent.com',
            clientSecret: "Y1l..........N7xkKd1x",
            refreshToken: "1/LFVq94wxy6uWMP............QqEWxIgOrJDtdun6zK6XiATCKT",
            accessToken: 'eyJ0eXAiOiJKV....................Zxd4uAPJlZqAdJwcHO3prmVSOtTo4PvEXkzhFJJo'
        })
    }
});

/*

 |--------------------------------------------------------------------------
 | POST /sendEmails
 |--------------------------------------------------------------------------
 */
 router.post('/sendToList', function(req, res){
    console.log(req.body);

    // setup e-mail data with unicode symbols
    var mailOptions = {
        from: 'connorleech@gmail.com', // sender address
        to: 'connorleech@gmail.com, baz@blurdybloop.com', // list of receivers
        subject: 'Hello ✔', // Subject line
        text: 'Hello world 🐴', // plaintext body
        html: '<b>Hello world 🐴</b>' // html body
    };

    // send mail with defined transport object
    transporter.sendMail(mailOptions, function(error, info){
        if(error){
            return console.log(error);
        }
        console.log('Message sent: ' + info.response);
    });

 });

 module.exports = router;
@andris9
Copy link
Member

andris9 commented Feb 13, 2016

Can't help much here, Gmail is really picky. Sometimes the client needs to be re-authorized, sometimes the correct scope ("https://mail.google.com/") is missing. Nodemailer just passes on the access token to Gmail as it is, or generates next one using the refresh token but does not modify any permissions etc.

@andris9 andris9 closed this as completed Feb 13, 2016
@connor11528
Copy link
Author

Mixed blessing, glad gmail has such good security but a pain for developers!

@ferchichi01
Copy link

Hi @connor11528 did you fix the issue

@AdamCraven
Copy link

AdamCraven commented Oct 16, 2019

For anyone having "unauthorized_client" issues whilst the following apply:

The issue was finally solved by following the documentation here: https://developers.google.com/identity/protocols/OAuth2ServiceAccount#delegatingauthority

In essence going to
https://admin.google.com/**yourgsuitedomain.com**/ManageOauthClients
Adding a client id (from service account) and scope: https://mail.google.com/ (final slash important)

Emails started working after that.

@jamezening
Copy link

@AdamCraven

Thank you for the message above.
I have been trouble shooting the crap out of the 2LO OAuth method for sending gmail through nodemailer. (the less secure, and 3LO methods work, but for easiest replication, I am trying to get the 2LO method to work)

I noticed that you have a G Suite account attached - my setup is through Firebase (and I am running the nodemailer code on Firebase Cloud Functions with a service account attached to a Firebase project) and am wondering if my woes are due to the fact that I do not have a G Suite account.

Thanks for any additional info you might have!

@jamezening
Copy link

I was able to try signing up for G Suite for the domain linked to our Firebase web project and this seems to solve the issue.
Thank you again for the tip above!

@FlacorLopes
Copy link

I forget to click on the gear icon at oauth2 playground and insert my OAuth Client ID and OAuth Client Secret.
Once i did it, it worked fine

image

@vnvyvu
Copy link

vnvyvu commented Jan 29, 2024

another case, make sure you have published the application:
image
then, do Step1: authorize APIs and Step 2: exchange authorization code for tokens again

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants