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


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

问题已解决

我已经使用php客户端库成功获得了一个OAuth2令牌,该令牌具有访问Google日历事件和Google联系人的适当范围(-edit-,在Google Developers控制台上启用了这两个API)。我可以使用该服务访问客户端的事件,但没有联系人服务。

如何使用此处引用的OAuth2令牌手动向Google Contacts API(GData-ver:3.0)发送授权的GET请求?

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

谷歌的回应:

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

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

一旦设置完成,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'));