如何发送 php GET 请求到 google 联系人 api v3 OAuth2

How to send php GET request to google contacts api v3 OAuth2

问题已解决

我已成功获得具有适当范围的 OAuth2 令牌,用于访问 Google 日历事件和 Google 联系人(-edit- APIs 在 Google 开发者控制台上启用),使用 php 客户端库。我可以使用该服务访问客户端的事件,但是没有联系人服务。

如何使用 OAuth2 令牌 as referenced here?

手动向 Google 联系人 API (GData-ver:3.0) 发送授权 GET 请求

这是在 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.

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

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

我想出了如何使用 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'));