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);