IMAP "Invalid Credentials" via GMail XOAUTH

I am trying to get to the IMAP server using OAuth using the PHP sample code provided by Google that uses the Zend Imap class but I cannot authenticate. Zend gives me an error:

Zend_Mail_Storage_Exception [ Error ]: cannot select INBOX, is this a valid transport? 

It’s annoying that this is a rather confusing error message, since it’s basically “Invalid credentials”. How do I know that? Debugging the actual commands sent to the IMAP socket, I see the following:

 string(44) "NO Invalid credentials ey9if1544983wid.142 " 

I tried using telnet and Ruby gmail_xoauth gem, which suggests that this is not a problem with the code, but something else.

Looking at the most basic level of all this, I get commands like this:

 TAG1 AUTHENTICATE XOAUTH R0VUIGh0dHBzOi8vbWFpbC5nb29nbGUuY29tL21h......etc 

Here I get NO Invalid credentials , and then:

 TAG2 SELECT "INBOX" 

This returns a BAD Unknown command and throws me out.

I tried looking for people having the same problem, but I only find questions and answers. There are several similar StackOverflow questions:

One post shows someone having the same issue in Python.

This post shows that someone is trying to be awkward and is doing this with OAuth 2 without a success report.

There is a thread in the GMail Google group that offers the error "Invalid credentials", which can be resolved by going to https://accounts.google.com/DisplayUnlockCaptcha for GMail accounts and https://www.google.com/a/[YOURDOMAIN .COM] / UnlockCaptcha , if you use Google Apps, but the latter simply said that my username and password were incorrect when they were clearly not, Using https://accounts.google.com/DisplayUnlockCaptcha worked fine - even if mine the account is a hosted application, not a plain old GMail - however, I still get the same errors when trying to log in with a sample PHP code, roviding Google.

I tried with various Google App accounts and a simple GMail account. I tried switching the IMAP server from imap.gmail.com to imap.googlemail.com, nothing matters.

  /** * Make the IMAP connection and send the auth request */ $imap = new Zend_Mail_Protocol_Imap('imap.googlemail.com', '993', true); $authenticateParams = array('XOAUTH', $initClientRequestEncoded); $imap->requestAndResponse('AUTHENTICATE', $authenticateParams); /** * Print the INBOX message count and the subject of all messages * in the INBOX */ $storage = new Zend_Mail_Storage_Imap($imap); echo '<h1>Total messages: ' . $storage->countMessages() . "</h1>\n"; 

For those who are interested, this is a specific PHP code that establishes a connection, all XOauth is processed by Google PHP in the same file, but I skipped it.

+6
source share
3 answers

I had the same error, and in the end I realized that I was using the wrong scope when requesting OAuth permissions.

You need to have ...

  • Volume = https://mail.google.com/
  • access_type = missing

And maybe IMAP is included in your Gmail account. Now everything seems good.

+4
source

You may have one problem: the third parameter is Zend_Mail_Protocol_Imap ('imap.googlemail.com', '993', true); should not be true. It should be a string like "SSL" or "TLS". I believe that you want “SSL” when going through port 993. This is only a boolean when it is FALSE.

Try replacing this first line as follows:

 $imap = new Zend_Mail_Protocol_Imap('imap.googlemail.com', '993', 'SSL'); 
+1
source

When I had the same error message, it was because

 $options['consumerSecret'] = $THREE_LEGGED_CONSUMER_SECRET_HMAC; 

I did this in the function and the variable $ THREE_LEGGED ... was not in the scope. Pretty stupid, and I'm sure you'd notice it, but just in case someone new reads it.

+1
source

Source: https://habr.com/ru/post/901294/


All Articles