【问题标题】:How to send php GET request to google contacts api v3 OAuth2如何向谷歌联系人 api v3 OAuth2 发送 php GET 请求
【发布时间】:2016-07-16 15:08:13
【问题描述】:

问题已解决

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

如何使用 OAuth2 令牌 as referenced here? 手动向 Google Contacts 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;

谷歌的回应:

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

【问题讨论】:

  • 是否在 Google Developers Console 中为您的应用启用了联系人 API?

标签: php curl gdata google-contacts-api google-oauth


【解决方案1】:

访问令牌不能用作您的 http GET 请求的参数。您需要在您的 Web 应用程序中设置它们。具体操作方法请见here

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

【讨论】:

  • 即使用服务对象作为api,并且没有联系人服务。我尝试使用与日历服务一起使用的经过身份验证的客户端,并尝试使用其可用方法发送具有不同故障级别的请求。我需要知道服务会/会做什么来发送请求,这就是我询问如何手动发送授权请求的原因。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-06
    相关资源
    最近更新 更多