使用PHP连接到Google Calendar v3 API


Connecting to Google Calendar v3 API with PHP

我完全迷路了…我所要做的就是摆脱这个错误信息,并登录到谷歌API。我已经创建了所有的键并将它们添加到/src/Google/Config.php。

PHP Fatal error:  Uncaught exception 'Google_Service_Exception' with message 'Error   calling GET  
https://www.googleapis.com/calendar/v3/users/me/calendarList/0qfrti6gst4q8hpl89utm8elno%40grou    p.calendar.google.com?key=AIzaSyAiR_4OsoheCPbd7tU2u3QrqbEW_a2uVCc: (401) Login Required'    
in C:''Bitnami''wampstack''apache2''htdocs''yac''google-api-php-
client''src''Google''Http''REST.php:79'nStack ...

这是我的(最可能)错误的脚本:

session_start();
set_include_path( get_include_path() . PATH_SEPARATOR . 'google-api-php-client/src' );
include_once('google-api-php-client/src');
require_once('google-api-php-client/src/Google/Client.php');
require_once ('google-api-php-client/src/Google/Service/Calendar.php');
$client = new Google_Client();
$client->setApplicationName("YAC_Calendar");
$client->setDeveloperKey(<MY KEY>);
$service = new Google_Service_Calendar($client);
$calendarListEntry = $service->calendarList->get('0qfrti6gst4q8hpl89utm8elno@group.calendar.google.com');
echo $calendarListEntry->getSummary();

您需要使用Oauth登录才能访问日历。我手头唯一的例子是一个使用谷歌分析API与PHP,如果你有任何切换到日历让我知道,我会看看我是否可以得到一个工作的例子。

<?php         
require_once 'Google/Client.php';     
require_once 'Google/Service/Analytics.php';       
session_start();      
$client = new Google_Client();
    $client->setApplicationName("Client_Library_Examples");
    $client->setDeveloperKey("{devkey}");  
    $client->setClientId('{clientid}.apps.googleusercontent.com');
    $client->setClientSecret('{clientsecret}');
    $client->setRedirectUri('http://www.daimto.com/Tutorials/PHP/Oauth2.php');
    $client->setScopes(array('https://www.googleapis.com/auth/analytics.readonly'));
    //For loging out.
    if ($_GET['logout'] == "1") {
    unset($_SESSION['token']);
       }   
    // Step 2: The user accepted your access now you need to exchange it.
    if (isset($_GET['code'])) {
        $client->authenticate($_GET['code']);  
        $_SESSION['token'] = $client->getAccessToken();
        $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
        header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
    }
    // Step 1:  The user has not authenticated we give them a link to login    
    if (!$client->getAccessToken() && !isset($_SESSION['token'])) {
        $authUrl = $client->createAuthUrl();
        print "<a class='login' href='$authUrl'>Connect Me!</a>";
        }        
    // Step 3: We have access we can now create our service
    if (isset($_SESSION['token'])) {
        print "<a class='logout' href='".$_SERVER['PHP_SELF']."?logout=1'>LogOut</a><br>";
        $client->setAccessToken($_SESSION['token']);
        $service = new Google_Service_Analytics($client);    
        // request user accounts
        $accounts = $service->management_accountSummaries->listManagementAccountSummaries();
       foreach ($accounts->getItems() as $item) {
        echo "Account: ",$item['name'], "  " , $item['id'], "<br /> 'n";        
        foreach($item->getWebProperties() as $wp) {
            echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WebProperty: ' ,$wp['name'], "  " , $wp['id'], "<br /> 'n";    
            $views = $wp->getProfiles();
            if (!is_null($views)) {
                foreach($wp->getProfiles() as $view) {
                //  echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;View: ' ,$view['name'], "  " , $view['id'], "<br /> 'n";    
                }
            }
        }
    } // closes account summaries
    }
 print "<br><br><br>";
 print "Access from google: " . $_SESSION['token']; 
?>

代码来自我的教程Google Oauth2 php