经过长时间的深入研究,我终于对Google API PHP客户端的配置和使用有了更深入地了解,这是基于分析数据的此教程。
所以,现在我终于以一种看起来合法和官方的方式进行了身份验证。我的自然想法是应该存在一个contrib/Google_ContactsService.php
文件,但令我惊讶的是,在其他32个服务类中都找不到它。
我感觉回到了原点。是否有任何方法可以以合法和官方的方式获取特定用户的联系人?(有很多教程,但都已过时且方法不当)。
编辑:我注意到库中有一个更新版本可用 此处,但在.../Service/文件夹中仍然找不到“联系人”服务。
编辑:
我的进展如下。最后一行由于从Google收到响应而失败: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.';