我经历了数小时的深刻痛苦,终于使用this tutorial(基于Google Analytics),更接近Google API API客户端的配置和使用。Google API PHP客户端 - 联系人服务
所以,现在我终于以一种看起来合法和正式的方式验证自己。我的自然想法是存在一个contrib/Google_ContactsService.php
,但令我惊讶的是,无法找到32个其他服务类别。
我感觉就像回到了零。有什么办法可以 - 合法和正式 - 获取特定用户的联系人? (那里有大量的教程,但都是过时的和黑客)。
编辑:我注意到有可用here库的新版本,但仍然没有任何“联系人”服务于.../Service/ folder
编辑被发现: 我迄今为止的进展。最后一行失败,回应谷歌:401. There was an error in your request.
- 我想这是因为缺乏权限(我没有要求联系人权限)。但是,如何在没有“Google_ContactsService.php”的情况下执行此操作?我搞不清楚了。见代码:
<?php
session_start();
/**
* Require the libaries
*/
require_once 'assets/php/Google/Google_Client.php';
require_once 'assets/php/Google/contrib/Google_AnalyticsService.php'; // from tutorial - I am supposed to get the Contacts Service, which is nowhere to find.
/**
* Set up the Google_Client
*/
$client = new Google_Client();
$client->setAccessType('online'); // default: offline
$client->setApplicationName($apiConfig['application_name']);
$client->setClientId($apiConfig['oauth2_client_id']);
$client->setClientSecret($apiConfig['oauth2_client_secret']);
$client->setRedirectUri($apiConfig['oauth2_redirect_uri']);
$client->setDeveloperKey($apiConfig['developer_key']); // API key
/**
* $service implements the client interface, has to be set before auth call
*/
$service = new Google_AnalyticsService($client);
/**
* Log out
*/
if (isset($_GET['logout'])) { // logout: destroy token
unset($_SESSION['google_token']);
exit('Logged out.');
}
/**
* Google auth code received
*
* Store access token
*/
if (isset($_GET['code'])) { // we received the positive auth callback, get the token and store it in session
$client->authenticate();
$_SESSION['google_token'] = $client->getAccessToken();
}
/**
* Set auth token
*/
if (isset($_SESSION['token'])) { // extract token from session and configure client
$token = $_SESSION['token'];
$client->setAccessToken($token);
}
/**
* If no token, redirect and auth
*/
if (!$client->getAccessToken()) { // auth call to google
$authUrl = $client->createAuthUrl();
header("Location: ".$authUrl);
exit;
}
/**
* Get contacts
*/
$access_token = json_decode($client->getAccessToken())->access_token;
$url = 'https://www.google.com/m8/feeds/contacts/default/full?alt=json&v=3.0&oauth_token='.$access_token;
$response = file_get_contents($url);
exit($response);
echo 'Hello, world.';
好的,谢谢!它会永远被弃用吗?我现在使用最新版本,并成功设置了API。未来会有这样的官方API吗?要么? – FooBar
https://developers.google.com/google-apps/contacts/v3/terms –
有没有人已经在新库中实现了这个功能?我正在尝试提出“自定义”请求,但我还没有成功:https://github.com/google/google-api-php-client/issues/446 – user1226868