2014-03-25 51 views
9

我經歷了數小時的深刻痛苦,終於使用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.'; 

回答

5

here

不幸的是,聯繫人API是上了年紀的GData的人之一,而這 庫是較新的API。您可以使用 庫的OAuth部分來請求範圍 (https://www.googleapis.com/auth/contacts.readonly),並使用令牌 發出請求,但您必須手動解析數據。 Zend 框架確實有Zend_Gdata類,可能會使讀取 結果更容易一些!

我找到了an example using the old library

+0

好的,謝謝!它會永遠被棄用嗎?我現在使用最新版本,併成功設置了API。未來會有這樣的官方API嗎?要麼? – FooBar

+0

https://developers.google.com/google-apps/contacts/v3/terms –

+1

有沒有人已經在新庫中實現了這個功能?我正在嘗試提出「自定義」請求,但我還沒有成功:https://github.com/google/google-api-php-client/issues/446 – user1226868

2

我能夠通過新庫得到這個工作......主要是。它顯然不像完整的服務實現那樣光滑,但它讓事情發展。

Git上面的帖子中提到的庫。 我開始與這裏的例子:https://developers.google.com/api-client-library/php/auth/web-app

oauth2callback.php(請注意,對此的完整路徑,必須對API的&驗證您的開發者控制檯或您的通話/憑據部分將無法上市):

<?php 

set_include_path(get_include_path() . PATH_SEPARATOR . '/data/www/unity/html/lib'); # The path where I git got google-api-php-client 
require_once 'google-api-php-client/src/Google/autoload.php'; 

$APPPATH = "/Applications/GFContacts"; 

session_start(); 

$client = new Google_Client(); 
$client->setAuthConfigFile('accountinfo.json'); # JSON config file downloaded from the credentials page of my project https://console.developers.google.com/project 
$client->setRedirectUri('https://' . $_SERVER['HTTP_HOST'] . $APPPATH . '/oauth2callback.php'); 
$client->addScope("https://www.googleapis.com/auth/contacts.readonly"); 

if (! isset($_GET['code'])) { 
    $auth_url = $client->createAuthUrl(); 
    header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL)); 
} else { 
    $client->authenticate($_GET['code']); 
    $_SESSION['access_token'] = $client->getAccessToken(); 
    $redirect_uri = 'https://' . $_SERVER['HTTP_HOST'] . $APPPATH; 
    header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL)); 
} 

?> 

然後index.php文件:

<?php 

set_include_path(get_include_path() . PATH_SEPARATOR . '/data/www/unity/html/lib'); # The path where I git got google-api-php-client 

require_once 'google-api-php-client/src/Google/autoload.php'; 
$APPPATH = "/Applications/GFContacts"; # relative path from server root 

session_start(); 

$client = new Google_Client(); 
$client->setAuthConfigFile('accountinfo.json'); # JSON config file downloaded from the credentials page of my project https://console.developers.google.com/project 
$client->addScope("https://www.googleapis.com/auth/contacts.readonly"); 

# Allow a param 'logout' to remove the access token - sometimes doing this helps debug issues 
if (isset($_REQUEST['logout'])) { 
    unset($_SESSION['access_token']); 
    $client->revokeToken(); 

    print "You're logged out of Google"; 

    exit; 
} 

if (isset($_SESSION['access_token']) && $_SESSION['access_token']) { 

    $access_token = json_decode($_SESSION['access_token'])->access_token; 

    $client->setAccessToken($_SESSION['access_token']); 

    $req = new Google_Http_Request("https://www.google.com/m8/feeds/contacts/default/full"); 
    $val = $client->getAuth()->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>"; 

} else { 
    $redirect_uri = 'https://' . $_SERVER['HTTP_HOST'] . $APPPATH . '/oauth2callback.php'; 
    header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL)); 
} 

?> 

祝你好運!自從這篇文章被更新以來,這已經有一段時間了,所以如果有人寫了一篇我很想知道的服務。在此期間,這應該讓你開始!

3

最近我不得不使用這個工具,在官方PHP Google Client中找不到聯繫人服務後,我爲Google Contacts API創建了一個(MIT許可的)PHP庫。

其中一個目標是真正簡化一些涉及的過程。因此,在設置庫後回答你的問題,以下代碼是檢索聯繫人所需的全部內容。

require_once '../../../vendor/autoload.php'; 
use rapidweb\googlecontacts\factories\ContactFactory; 
$contacts = ContactFactory::getAll(); 
if (count($contacts)) { 
    echo 'Test retrieved '.count($contacts).' contacts.'; 
} else { 
    echo 'No contacts retrieved!'; 
} 

該庫需要一點工作,但對於基本聯繫人檢索,創建和更新工作良好。如果需要,它也可以通過composer進行安裝。只需將以下內容添加到您的composer.json並運行composer update

{ 
    "require": { 
     "rapidwebltd/php-google-contacts-v3-api": "dev-master" 
    } 
} 

GitHub上提供了更多安裝說明和示例。

GitHub鏈接:https://github.com/rapidwebltd/php-google-contacts-v3-api

相關問題