如何发送php GET请求谷歌联系人api v3 OAuth2

时间:2016-03-29 07:22:45

标签: php curl gdata google-contacts google-oauth2

解决问题

我已成功获取OAuth2令牌,其中包含使用php客户端库访问Google Calendar事件和Google通讯录( -edit - 并在Google Developers控制台上启用了两个API)的范围。我可以使用该服务访问客户端的事件,但是联系人没有服务。

如何使用OAuth2令牌as referenced here?

手动将授权的GET请求发送到Google Contacts API(GData-ver:3.0)

这是在Joomla 3.x中,但我直接使用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;

Google的回复:

401. That's an error. There was an error in your request. That's all we know.

2 个答案:

答案 0 :(得分:0)

访问令牌不能用作http GET请求的参数。您需要在Web应用程序中进行设置。如何做到这一点可以找到here

设置完成后,https://www.google.com/m8/feeds/contacts/default/full?v=3.0的GET请求应该能够正常工作

答案 1 :(得分:0)

我想出了如何使用PHP客户端库中的Google_Http_Request对象来完成它。

  $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'));