I have successfully obtained an OAuth2 token with proper scopes for accessing Google Calendar Events and Google Contacts (-edit- with both APIs enabled on Google Developers console), using the php client library. I am able to access events with the client using the service however there is no service for contacts.
How can I manually send an authorized GET request to the Google Contacts API (GData-ver:3.0) with the OAuth2 token as referenced here?
This is in Joomla 3.x but I'm working directly with php.
$retrieveURL = 'https://www.google.com/m8/feeds/contacts/default/full?access_token=' . urlencode($tokens->access_token) . '&v=3.0';
$cURL = curl_init();
curl_setopt($cURL, CURLOPT_URL, $retrieveURL);
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($cURL, CURLINFO_HEADER_OUT, 1);
curl_setopt($cURL, CURLOPT_SSL_VERIFYPEER, false);
$gContacts = curl_exec($cURL);
if ($errno = curl_errno($cURL)) {
$error_message = curl_strerror($errno);
echo("cURL error ({$errno}):\n {$error_message}:\n ");
var_dump(curl_getinfo($cURL, CURLINFO_HEADER_OUT));
}
var_dump($gContacts);
return $gContacts;
The response from Google:
401. That's an error. There was an error in your request. That's all we know.
I figured out how to do it using Google_Http_Request
object from the PHP client library.
$retrieveURL = 'https://www.google.com/m8/feeds/contacts/default/full?alt=json';
$httpRequest = new Google_Http_Request($retrieveURL, 'GET');
/**
* this will first check and renew access_token if needed
* then sets the access token in the request header
**/
$client->getAuth()->sign($httpRequest);
/**
* modify the request to work with Google Contacts API v3.0
**/
$httpRequest->setBaseComponent('https://www.google.com');
$httpRequest->setRequestHeaders(['Gdata-version' => '3.0']));
/**
* Send the request and assign the response
**/
$response = $client->getIo()->makeRequest($httpRequest);
// replace problematic '$'s with 'G's in json response string
$responseString = implode('G', explode('$', $response->getResponseBody()));
$responseBody = json_decode($responseString);
$responseArray = $responseBody->feed->entry;
foreach ((array)$responseArray as $entry) {
$gdName = (isset($entry->gdGname->gdGfullName->Gt)) ? $entry->gdGname->gdGfullName->Gt : 'No Name';
$gdFName = (isset($entry->gdGname->gdGgivenName->Gt)) ? $entry->gdGname->gdGgivenName->Gt : 'No First Name';
$gdLName = (isset($entry->gdGname->gdGfamilyName->Gt)) ? $entry->gdGname->gdGfamilyName->Gt : 'No Last Name';
$gdEmail = (isset($entry->gdGemail[0]->address)) ? $entry->gdGemail[0]->address : 'No Email Address';
$gdPhone = (isset($entry->gdGphoneNumber[0]->Gt)) ? $entry->gdGphoneNumber[0]->Gt : 'No Phone Number';
$gdOrgName = (isset($entry->gdGorganization[0]->gdGorgName->Gt)) ? $entry->gdGorganization[0]->gdGorgName->Gt : 'No Company Name';
$output[] = [
"name" => $gdName,
"first_name" => $gdFName,
"last_name" => $gdLName,
"email" => $gdEmail,
"phone" => $gdPhone,
"company" => $gdOrgName,
];
}
$app = $this->app;
// store the contact and redirect to view
$_SESSION["gdContacts"] = $output;
$app->redirect(JRoute::_('web/content/google/contacts'));