Foo*_*Bar 9 php api google-api google-contacts-api google-api-php-client
经过几个小时的深度痛苦之后,我终于使用本教程(尽管基于Google Analytics),更接近Google的API PHP客户端的配置和使用.
所以,现在我终于以一种看似合法且官方的方式认证自己.我的自然想法是存在一个contrib/Google_ContactsService.php,但令我惊讶的是,它无处可寻找其他32个服务类别.
我觉得自己又回头了.我有什么方法 - 合法和正式 - 获取特定用户的联系人?(大量的教程,但都过时和hacky).
编辑:我注意到有库的更新版本可在这里,但仍然没有在被发现的"通讯录"服务...... /服务/文件夹
编辑:
到目前为止我的进展.最后一行因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.';
Run Code Online (Sandbox Code Playgroud)
从这里:
不幸的是,contacts API是较旧的GData之一,而这个库是针对较新的API.您可以使用库的OAuth部分来请求范围(https://www.googleapis.com/auth/contacts.readonly),并使用令牌发出请求,但您必须手动解析数据.Zend Framework确实有Zend_Gdata类,可能会使读取结果更容易一些!
我找到了一个使用旧库的例子.
| 归档时间: |
|
| 查看次数: |
11670 次 |
| 最近记录: |