0

我在谷歌日历应用中使用PHP客户端库。要获取访问令牌,请求输入用户名和密码,然后选择允许访问选项。我希望一步完成这个过程。为此我使用了https://www.google.com/accounts/ClientLogin \ url并使用curl发送用户名和密码进行身份验证。它返回SID和Auth值。请让我知道如何使用Auth值来获取访问令牌。获取谷歌日历API中的代码时出错

curl https://www.google.com/accounts/ClientLogin \ 
--data-urlencode [email protected] --data-urlencode Passwd=new+foundland \ 
-d accountType=GOOGLE \ 
-d source=Google-cURL-Example \ 
    -d service=lh2 

    Response:- 

    SID=DQAAAHYBADCv2pSv7nfl- 
    LSID=EUBBBIaBADCl- 
    Auth=EUBBIacAAADK-kN- 

    I'm using the follwing code but it is 2 steps varification: 

    <?php 
    require_once 'google-api-php-client/src/Google_Client.php'; 
    require_once 'google-api-php-client/src/contrib/Google_PlusService.php'; 

    // Set your cached access token. Remember to replace $_SESSION with a 
    // real database or memcached. 
    session_start(); 

    $client = new Google_Client(); 
    $client->setApplicationName('Google+ PHP Starter Application'); 
    // Visit https://code.google.com/apis/console?api=plus to generate your 
    // client id, client secret, and to register your redirect uri. 
    $client->setClientId('insert_your_oauth2_client_id'); 
$client->setClientSecret('insert_your_oauth2_client_secret'); 
$client->setRedirectUri('insert_your_oauth2_redirect_uri'); 
    $client->setDeveloperKey('insert_your_simple_api_key'); 
$plus = new Google_PlusService($client); 

    if (isset($_GET['code'])) { 
    $client->authenticate(); 
    $_SESSION['token'] = $client->getAccessToken(); 
    $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; 
    header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL)); 
    } 

    if (isset($_SESSION['token'])) { 
$client->setAccessToken($_SESSION['token']); 
    } 

    if ($client->getAccessToken()) { 
    $activities = $plus->activities->listActivities('me', 'public'); 
    print 'Your Activities: <pre>' . print_r($activities, true) . '</pre>'; 

    // We're not done yet. Remember to update the cached access token. 
    // Remember to replace $_SESSION with a real database or memcached. 
    $_SESSION['token'] = $client->getAccessToken(); 
    } else { 
    $authUrl = $client->createAuthUrl(); 
    print "<a href='$authUrl'>Connect Me!</a>"; 
} 


    I wanna to show all the events from my account to on my site. Please let me know 
    how can I accomplish this. 

    Thanks. 

回答

0

您正在使用的PHP客户端库不支持ClientLogin,它使用的是Oauth2,而这是推荐的方法。

但是,还有另一个PHP库ZEND,它有一个使用clientLogin的日历库。 http://goo.gl/Rhet6

// Parameters for ClientAuth authentication 
$service = Zend_Gdata_Calendar::AUTH_SERVICE_NAME; 
$user = "[email protected]"; 
$pass = "pa$$w0rd"; 

// Create an authenticated HTTP client 
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service); 

// Create an instance of the Calendar service 
$service = new Zend_Gdata_Calendar($client);