Gmail API: Oauth2, Zend IMAP

486 views
Skip to first unread message

Adrian Stanescu

unread,
Sep 27, 2012, 10:43:04 AM9/27/12
to oauth...@googlegroups.com
Hello all,

I'm having an issue with the gmail api.

I'm using this php code straight off google code:
It includes Zend for IMAP access.

The above code works just fine if I generate an access token using oauth2.py (as advertised):

But if i try to generate an access token using apiClient.php (from the standard google-api-php-client):
and inserting that into Zend's IMAP request, i get connection fail. 

The access token is generated (i echo it and it's there), but when i try to connect with Zend's IMAP to google, it's just going to output connection failed.

Some code for clarification (some fields hidden for security reasons):

//STANDARD GOOGLE API CONNECT

$client = new apiClient();
$client->setApplicationName("GMail App");
$client->setClientId('HIDDEN');
$client->setClientSecret('HIDDEN');
$client->setRedirectUri('http://webclient.heft.cc/p/o/index.php');
$client->setDeveloperKey('HIDDEN');

//MY EMAIL
$email = 'stanesc...@gmail.com'; 

//I GET THE ACCESS TOKEN, I CONVERT IT TO UTF-8, AND THEN I JSON DECODE IT
$tok = json_decode(mb_convert_encoding($client->getAccessToken(), 'utf-8'));
$tok2 = $tok->{'access_token'};

// $oauth2py = 'ACCESS TOKEN FROM OAUTH2.PY WHICH WORKS';

tryImapLogin($email, $tok2);

/**
* Make the IMAP connection and send the auth request
*/
function tryImapLogin($email, $accessToken) {
  $imap = new Zend_Mail_Protocol_Imap('imap.gmail.com', '993', true);
  if (oauth2Authenticate($imap, $email, $accessToken)) {
    echo '<h1>Successfully authenticated!</h1>';
    showInbox($imap);
  } else {
    echo '<h1>Failed to login</h1>';
  }
}

You can test it out for yourself here:

If instead of:
tryImapLogin($email, $tok2);
i generate an auth code using oauth2.py and i call:
tryImapLogin($email, $oauth2py);
it works!

Any help would be immensely appreciated!

Adrian.

Jinhui Du

unread,
Oct 1, 2012, 3:24:14 PM10/1/12
to oauth...@googlegroups.com
Hi Adrian,

You may also try to reach https://groups.google.com/forum/#!forum/google-api-php-client which is specific for the php client library support.



Adrian.

--
 
 

Adrian Stanescu

unread,
Oct 2, 2012, 2:49:21 AM10/2/12
to oauth...@googlegroups.com
Thank you Jinhui Du.
Reply all
Reply to author
Forward
0 new messages