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.
$imap = new Zend_Mail_Protocol_Imap('imap.googlemail.com', '993', true); $authenticateParams = array('XOAUTH', $initClientRequestEncoded); $imap->requestAndResponse('AUTHENTICATE', $authenticateParams); $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.