2013-01-11 153 views
0

我對谷歌api,特別是谷歌日曆api感到困惑。看來我發現每個例子都希望用戶連接並通過返回URL返回。我想要做的就是用谷歌日曆api在背景中獲取信息,顯示數據,甚至創建數據。我會願意用Javascript/JSON或通過PHP來做到這一點。谷歌日曆api與PHP訪問

這裏是我發現的例子:

<?php 
session_start(); 
require_once dirname(__FILE__).'/GoogleClientApi/Google_Client.php'; 
require_once dirname(__FILE__).'/GoogleClientApi/contrib/Google_AnalyticsService.php'; 

$scriptUri = "http://".$_SERVER["HTTP_HOST"].$_SERVER['PHP_SELF']; 

$client = new Google_Client(); 
$client->setAccessType('online'); // default: offline 
$client->setApplicationName('My Application name'); 
$client->setClientId('INSERT HERE'); 
$client->setClientSecret('INSERT HERE'); 
$client->setRedirectUri($scriptUri); 
$client->setDeveloperKey('INSERT HERE'); // API key 

// $service implements the client interface, has to be set before auth call 
$service = new Google_AnalyticsService($client); 

if (isset($_GET['logout'])) { // logout: destroy token 
    unset($_SESSION['token']); 
    die('Logged out.'); 
} 

if (isset($_GET['code'])) { // we received the positive auth callback, get the token and store it in session 
    $client->authenticate(); 
    $_SESSION['token'] = $client->getAccessToken(); 
} 

if (isset($_SESSION['token'])) { // extract token from session and configure client 
    $token = $_SESSION['token']; 
    $client->setAccessToken($token); 
} 

if (!$client->getAccessToken()) { // auth call to google 
    $authUrl = $client->createAuthUrl(); 
    header("Location: ".$authUrl); 
    die; 
} 
echo 'Hello, world.'; 

回答

0

我想通了這一點。如果任何人都好奇這是如何工作的,無論您使用的是哪種谷歌API,您都需要先通過瀏覽器訪問,除非您已經擁有訪問令牌。在此之後,您可以從他們的圖書館獲取您需要的任何信息。