1
votes

I have been using the Google Calendar API for a number of years with great success. I use requests from php using curl.

I've been trying to achieve the same with the contacts api. At the moment all I want to do is retrieve a list of contacts as json for a user I've retrieved a token for through OAuth2. I'm using the simple code below for testing.

 $accessToken = "thebiglongaccesstoken1234";
 $userMail = "[email protected]";
 $requestURL ="https://www.google.com/m8/feeds/contacts/".$userMail."/full?v=3.0&alt=json";
 $headers = array("Authorization: OAuth ".$accessToken);

 $ch = curl_init();

 curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); 
 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
 curl_setopt($ch, CURLOPT_URL, $requestURL);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

 $response = curl_exec($ch);
 $responseArray = json_decode($response, TRUE);

 var_dump($responseArray);
 print '<br/>Response: <pre>' . print_r($responseArray, true) . '</pre>';
 print 'curl Error: <pre>' . print_r(curl_error($ch), true) . '</pre>';

the response in the browser is:

NULL Response: curl Error:

I can get this request to work in the google OAuth 2.0 playground.

I don't want to use a library because all I eventually want to do is find a users contact by email address to get the phone number. I have made my own library of functions for the calendar so don't want to add extra unused stuff. It should only require a few lines of code.

I think I've just stared at it too long now and am missing the obvious. Any help appreciated.

UPDATE:

I got this out using curl_getinfo()

Array ( [url] => https://www.google.com/m8/feeds/contacts/[email protected]/full?v=3.0&alt=json [content_type] => text/html; charset=utf-8 [http_code] => 401 [header_size] => 457 [request_size] => 208 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0.1244 [namelookup_time] => 0.000837 [connect_time] => 0.00306 [pretransfer_time] => 0.015428 [size_upload] => 0 [size_download] => 11875 [speed_download] => 95458 [speed_upload] => 0 [download_content_length] => -1 [upload_content_length] => 0 [starttransfer_time] => 0.123992 [redirect_time] => 0 [certinfo] => Array ( )

)

1
Can you print the output before passing it into json_decode? The Contacts API generally gives plain-text errors. Also a response status code would be helpful too.Blake O'Hare
If I try and do anything with $response such as print it or var_dump I get the broken robot google screen with a 401or 404 error and the message "There was an error in your request. That's all we know." a var_dump on curl error shows an empty string.colin_martin

1 Answers

2
votes

Seeing that it's a 401, I notice your authorization header isn't quite correct. Change your Authorization header to "Authorization: Bearer " . $accessToken instead of "Authorization: OAuth " . $accessToken.

If you are still getting a 401, then you'll need to get a new Access Token.