XOAuth2 for Gmail error Invalid Credential

I am using passport.js for my authentication service to get oauth2 access token from google(https://github.com/jaredhanson/passport-google-oauth#usage-of-oauth-20), I can get the access token with the scopes: https://mail.google.com/, https://www.googleapis.com/auth/userinfo.email. But, when I am trying to connect to imap (using https://github.com/mscdex/node-imap), I am getting the error: 'Invalid credentials'.

Here is the snippet code I am using:

var authData = [
"user=" + ('test@gmail.com' || ""),
"auth=Bearer " + 'ya29.LgBFSGtnPtfHLxkAAADjgeEFpUC9nD31kP5BqLKuH1MO3e_TRSdRTjEVKqQ-GQ',
"",
""];
var xoauth2_token = new Buffer(authData.join("x01"), "utf-8").toString("base64");

var imap = new Imap({
            user: ‘test@gmail.com',
            xoauth2: xoauth2_token,
            host: 'imap.gmail.com',
            port: 993,
            tls: true,
            debug: console.log
        });
        imap.on('ready', function() {
            imap.openBox('INBOX', true, function() {
                var f = imap.seq.fetch(1);
                f.on('message', function(m) {
                    m.once('attributes', function(attrs) {
                        console.log(attrs);
                    });
                });
                f.on('end', function() {
                    imap.end();
                });
            });
        });
        imap.connect();

The headers of the request and the response:

[connection] Connected to host
<= '* OK Gimap ready for requests from 212.143.212.222 ce43mb7026158web'
=> 'A0 CAPABILITY'
<= '* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 XYZZY SASL-IR AUTH=XOAUTH AUTH=XOAUTH2 AUTH=PLAIN AUTH=PLAIN-CLIENTTOKEN'
<= 'A0 OK Thats all she wrote! ce43mb7026158web'
=> 'A1 AUTHENTICATE XOAUTH2     dXNlcj11YXBwZmFjdG9yeUBnbWFpbC5jb20BYXV0aD1CZWFyZXIgeWEyOS5MZ0JGU0d0blB0ZkhMeGtBQUFEamdlRUZwVUM5bkQzMWtQNUJxTEt1SDFNTzNlX0dMU2RSVGpFVktxUS1HUQEB'
<= '+ eyJzdGF0dXMiOiI0MDAiLCJzY2hlbWVzIjoiQmVhcmVyIiwic2NvcGUiOiJodHRwczovL21haWwuZ29vZ2xlLmNvbS8ifQ=='
=> 'rn'
<= 'A1 NO [ALERT] Invalid credentials (Failure)'

I tried with specify the empty password, and without, with the user or without, noting seems to work.

I also followed the examples on google site: https://developers.google.com/gmail/xoauth2_libraries

In my google account I can see the application I am using to connect to google and get the token, with the authorization of manage and view emails, and base details about the user profile.

What am I doing wrong?


I was having the same problem. It was related with the OAuth 2.0 Scopes.

First, I was asking permission to access:

  • https: // www.googleapis.com/auth/userinfo.profile
  • https: // www.googleapis.com/auth/userinfo.email
  • With those scopes I got that error.

    "A1 NO [ALERT] Invalid credentials (Failure)"

    At the end I found at Using OAuth 2.0

    The scope for IMAP and SMTP access is https://mail.google.com/ .

    I requested permissions with that scope and things start working.

    var params = {
        response_type: "code",
        client_id: "YOUR_CLIENT_ID",
        redirect_uri: "REDIRECT_URI",
        scope: 'https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email https://mail.google.com/',
        approval_prompt: 'force'
    };
    

    Regards,

    jcvalerio

    链接地址: http://www.djcxy.com/p/83208.html

    上一篇: 缺少必需的参数:重定向

    下一篇: XOAuth2 for Gmail错误证书无效