谷歌与谷歌联系api - 无法设置它

时间:2012-05-17 12:41:28

标签: php google-api google-contacts

我无法让它发挥作用。我正在使用google php库中的示例中的simple.php。这是页面:

require_once '../../src/apiClient.php';
session_start();

$client = new apiClient();
$client->setApplicationName('Google Contacts PHP Sample');
$client->setScopes("http://www.google.com/m8/feeds/");
// Documentation: http://code.google.com/apis/gdata/docs/2.0/basics.html
// Visit https://code.google.com/apis/console?api=contacts to generate your
// oauth2_client_id, oauth2_client_secret, and register your oauth2_redirect_uri.
// $client->setClientId('insert_your_oauth2_client_id');
// $client->setClientSecret('insert_your_oauth2_client_secret');
// $client->setRedirectUri('insert_your_redirect_uri');
// $client->setDeveloperKey('insert_your_developer_key');

$client->setClientId('xxxxxxxxxxx.apps.googleusercontent.com');
$client->setClientSecret('xxxxxxxxxxxxxx');
$client->setRedirectUri('https://www.xxxxxxxxxxxxxxx.com/oauth2callback');
$client->setDeveloperKey('xxxxxxxxxxxxxxxxx');

if (isset($_GET['code'])) {
  $client->authenticate();
  $_SESSION['token'] = $client->getAccessToken();
  $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
  header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}

if (isset($_SESSION['token'])) {
 $client->setAccessToken($_SESSION['token']);
}

if (isset($_REQUEST['logout'])) {
  unset($_SESSION['token']);
  $client->revokeToken();
}

if ($client->getAccessToken()) {
  $req = new apiHttpRequest("https://www.google.com/m8/feeds/contacts/default/full");
  $val = $client->getIo()->authenticatedRequest($req);

  // The contacts api only returns XML responses.
  $response = json_encode(simplexml_load_string($val->getResponseBody()));
  print "<pre>" . print_r(json_decode($response, true), true) . "</pre>";

  // The access token may have been updated lazily.
  $_SESSION['token'] = $client->getAccessToken();
} else {
  $auth = $client->createAuthUrl();
}

if (isset($auth)) {
    print "<a class=login href='$auth'>Connect Me!</a>";
  } else {
    print "<a class=logout href='?logout'>Logout</a>";
}

在页面中我收到了注销消息,我从获取中收到错误。我在apache错误日志中看到:

PHP Warning:  simplexml_load_string(): <ins>That&#39;s all we know.</ins></p></body></html> in /www/xxxxxxxxxxxxxxx.com/docs/oauth2callback/google-api-php-client/examples/contacts/index.php on line 58

我正在尝试几个小时才能得到它。首先尝试在javascript中,不能。现在尝试php。

更新: 最后我发现这个链接完美无缺: http://25labs.com/import-gmail-or-google-contacts-using-google-contacts-data-api-3-0-and-oauth-2-0-in-php/

1 个答案:

答案 0 :(得分:0)

变化

$response = json_encode(simplexml_load_string($val->getResponseBody()));
print "<pre>" . print_r(json_decode($response, true), true) . "</pre>";

$response = $val->getResponseBody();
print "<pre>" . print_r($response) . "</pre>";

这将打印出联系人的第一页,但我似乎无法获得更多页面