I have scoured the web trying to find an implementation of accessing gmail via php using an oauth token. I found many places that say it is supported, but I can’t get it to work. I’m using the zend framework to access the contact list and I got that working great, but gmail is throwing me for a loop. I keep getting ‘Unauthorized’ error message.
Here is my oauth bit.
// Multi-scoped token.
$SCOPES = array(
'http://www.google.com/m8/feeds/',
'https://mail.google.com/mail/feed/atom/'
);
$oauthOptions = array(
'requestScheme' => Zend_Oauth::REQUEST_SCHEME_HEADER,
'version' => '1.0',
'consumerKey' => $CONSUMER_KEY,
'consumerSecret' => $CONSUMER_SECRET,
'signatureMethod' => 'HMAC-SHA1',
'callbackUrl' => 'https://' . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'],
'requestTokenUrl' => 'https://www.google.com/accounts/OAuthGetRequestToken',
'userAuthorizationUrl' => 'https://www.google.com/accounts/OAuthAuthorizeToken',
'accessTokenUrl' => 'https://www.google.com/accounts/OAuthGetAccessToken'
);
$consumer = new Zend_Oauth_Consumer($oauthOptions);
// When using HMAC-SHA1, you need to persist the request token in some way.
// This is because you'll need the request token's token secret when upgrading
// to an access token later on. The example below saves the token object as a session variable.
if (!isset($_SESSION['ACCESS_TOKEN'])) {
if (!empty($_GET) && isset($_SESSION['REQUEST_TOKEN'])) {
$_SESSION['ACCESS_TOKEN'] = serialize($consumer->getAccessToken($_GET, unserialize($_SESSION['REQUEST_TOKEN'])));
unset($_SESSION['REQUEST_TOKEN']);
}
else {
$_SESSION['REQUEST_TOKEN'] = serialize($consumer->getRequestToken(array('scope' => implode(' ', $SCOPES))));
//$consumer->redirect(Array('hd' => 'sxrmedical.com'));
$consumer->redirect();
//$approvalUrl = $consumer->getRedirectUrl(Array('hd' => 'default'));
}
}