I'm trying to write a code to manage my shared contacts so I followed all
available tutorials to come up with the following error:
ERROR:Expected response code 200, got 401
Authorization required
Error 401
However, when I change to last few lines to work with Google Docs it works
perfectly. It looks like I'm doing something wrong with my code for Contacts
API. Take a look:
*test.php
*
<?
session_start();
ini_set("display_errors", 1);
require_once 'Zend/Loader.php';
require_once 'Zend/Oauth/Consumer.php';
$CONSUMER_KEY = 'www.stingerit.com';
$CONSUMER_SECRET = 'WHymWg9mjRlRNsA4Uhxxxxxx';
$BASE_URL = 'http://www.stingerit.com/helloworld';
$STORE_PATH = "/tmp/_php_consumer_test";
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_Gapps');
Zend_Loader::loadClass('Zend_Gdata_Query');
$SCOPES = array(
'https://www.google.com/m8/feeds',
'https://docs.google.com/feeds/',
'https://spreadsheets.google.com/feeds/'
);
$options = array(
'requestScheme' => Zend_Oauth::REQUEST_SCHEME_HEADER,
'version' => '1.0',
'signatureMethod' => 'HMAC-SHA1',
'consumerKey' => $CONSUMER_KEY,
'consumerSecret' => $CONSUMER_SECRET,
'callbackUrl' => 'http://www.stingerit.com/helloworld/test1.php',
'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($options);
if (!isset($_SESSION['ACCESS_TOKEN'])) {
$_SESSION['REQUEST_TOKEN'] =
serialize($consumer->getRequestToken(array('scope' => implode(' ',
$SCOPES))));
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$approvalUrl = $consumer->getRedirectUrl(array('hd' => 'default'));
echo "<a href=\"$approvalUrl\">Grant access";
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
?>
*test1.php*
<?
require_once 'Zend/Oauth/Consumer.php';
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Http_Client');
Zend_Loader::loadClass('Zend_Gdata_Query');
Zend_Loader::loadClass('Zend_Gdata_Feed');
session_start();
global $_SESSION;
$CONSUMER_KEY = 'www.stingerit.com';
$CONSUMER_SECRET = 'WHymWg9mjRlRNsA4UhBWo67f';
// Multi-scoped token.
$SCOPES = array(
'https://www.google.com/m8/feeds',
'https://docs.google.com/feeds/',
'https://spreadsheets.google.com/feeds/'
);
$oauthOptions = array(
'requestScheme' => Zend_Oauth::REQUEST_SCHEME_HEADER,
'version' => '1.0',
'consumerKey' => $CONSUMER_KEY,
'consumerSecret' => $CONSUMER_SECRET,
'signatureMethod' => 'HMAC-SHA1',
'callbackUrl' => 'http://www.stingerit.com/helloworld/test1.php',
'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);
if (!isset($_SESSION['ACCESS_TOKEN'])) {echo "in first if /br";
if (!empty($_GET) && isset($_SESSION['REQUEST_TOKEN'])) { echo "gotm";
$_SESSION['ACCESS_TOKEN'] = serialize($consumer->getAccessToken($_GET,
unserialize($_SESSION['REQUEST_TOKEN'])));
}
}
if (isset($_SESSION['ACCESS_TOKEN'])) {
$accessToken = unserialize($_SESSION['ACCESS_TOKEN']);
}else {
exit;
}
$httpClient = $accessToken->getHttpClient($oauthOptions);
//$client = new Zend_Gdata_Docs($httpClient, "yourCompany-YourAppName-v1");
// Retrieve user's list of Google Docs
//$feed = $client->getDocumentListFeed();
try {
$gdata = new Zend_Gdata($client);
$gdata->setMajorProtocolVersion(3);
$query = new Zend_Gdata_Query('
http://www.google.com/m8/feeds/contacts/default/full');
$feed = $gdata->getFeed($query);
// foreach ($feed->entries as $entry) {
// echo "$entry->title\n";
// }
} catch (Exception $e) {
die('ERROR:' . $e->getMessage());
}
?>
You can find this example here:
http://www.stingerit.com/helloworld/test1.php
Why do I get this error?
--
You received this message because you are subscribed to the Google
Groups "Google Contacts, Shared Contacts and User Profiles APIs" group.
To post to this group, send email to
[email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://code.google.com/apis/contacts/community/forum.html