简体   繁体   English

使用php的google contact api-无法设置

[英]google contact api with php - can't get it set up

I just can't get it to work. 我就是无法正常工作。 I'm using the simple.php from the examples in google php library. 我正在使用Google php库中示例中的simple.php。 this is the page: 这是页面:

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>";
}

in the page i get the logout message, and i'm getting an error from the fetch. 在页面中,我得到注销消息,并且从获取中获取错误。 i see in apache error logs: 我在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

i'm trying a few hours to get it. 我正在尝试几个小时才能得到它。 first tried in javascript, couldn't. 第一次尝试用javascript,不能。 now trying php. 现在尝试php。

UPDATE: in the end i found this link which works perfectly: http://25labs.com/import-gmail-or-google-contacts-using-google-contacts-data-api-3-0-and-oauth-2-0-in-php/ 更新:最后,我发现此链接非常有效: http : //25labs.com/import-gmail-or-google-contacts-using-google-contacts-data-api-3-0-and-oauth-2 -0-in-php /

change 更改

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

to

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

this will print out the first page of contacts, but i cant seem to get more pages 这将打印出联系人的第一页,但我似乎无法获得更多页面

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM