在google calendar api中获取代码时出错

时间:2013-04-10 22:28:48

标签: google-api google-calendar-api google-apps google-api-php-client

我在谷歌日历app中使用php客户端库。要获取访问令牌,它会询问用户名和密码,然后允许访问选项。我想一步到位。为此,我使用https://www.google.com/accounts/ClientLogin \ url并使用curl发送用户名和密码进行身份验证。它返回SID和Auth值。请告诉我如何使用Auth值来获取访问令牌。

 curl https://www.google.com/accounts/ClientLogin \
 --data-urlencode Email=brad.gushue@example.com --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.

1 个答案:

答案 0 :(得分:0)

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

然而,还有另一个php库,ZEND有一个使用clientLogin的日历库。 http://goo.gl/Rhet6

// Parameters for ClientAuth authentication
$service = Zend_Gdata_Calendar::AUTH_SERVICE_NAME;
$user = "sample.user@gmail.com";
$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);