2013-01-11 112 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,您都需要先通过浏览器访问,除非您已经拥有访问令牌。在此之后,您可以从他们的图书馆获取您需要的任何信息。