仅使用令牌登录谷歌日历

时间:2016-07-20 05:55:37

标签: php google-calendar-api

我将google日历与我的应用程序成功集成。 第一次,我成功验证并正确地在谷歌日历上创建所有事件。

现在,第二次我不想进行身份验证,我只想点击一个按钮,然后我的应用程序中的所有新事件都应该在Google日历上创建。

我猜它会由刷新令牌完成。 我收到了令牌。 请参阅下面的代码。

<?php
session_start();        
require_once('vendor/google/apiclient/src/Google/autoload.php');
require_once 'vendor/google/apiclient/src/Google/Client.php';
require_once 'vendor/google/apiclient/src/Google/Service/Calendar.php';



$scopes ="https://www.googleapis.com/auth/calendar.readonly";   

$client_id = '';
$Email_address = '';     
$key_file_location = ''; 
$client_secret = "";    
$redirect_uri = "";
$key = file_get_contents($key_file_location);


$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->setAccessType('offline');   // Gets us our refreshtoken

$client->setScopes(array('https://www.googleapis.com/auth/calendar.readonly'));

$cred = new Google_Auth_AssertionCredentials(    
$Email_address,      
array($scopes),     
$key         
);      
$client->setAssertionCredentials($cred);
if($client->getAuth()->isAccessTokenExpired()) {        
$client->getAuth()->refreshTokenWithAssertion($cred); 
$_SESSION['token'] = $client->getAccessToken();
$client->setAccessToken($_SESSION['token']);
}
echo "Token".$_SESSION['token'];
if ($client->getAccessToken()) {
 $service = new Google_Service_Calendar($client);
 $events = $service->events->listEvents('primary');
 while(true) {

foreach ($events->getItems() as $event) {

echo $event->getSummary() . " ==> ";
echo $event->end->dateTime;
echo "<br>";
}
$pageToken = $events->getNextPageToken();
if ($pageToken) {
$optParams = array('pageToken' => $pageToken);
$events = $service->events->listEvents('primary', $optParams);
} else {
break;
}
}
}  


?>

我在这里获得了令牌,但现在我想在我的谷歌日历中登录而不再使用身份验证。因为我第一次这样做了。

现在,我可以使用令牌登录我的Google帐户吗? 如果是,那我该怎么办呢?

我第一次验证用户,因为这是我的代码。

 <?php
 session_start();
 $client = new Google_Client();
 $client->setApplicationName("ABC-APP");
 $client->setClientId("");
 $client->setClientSecret('');
 $client->setRedirectUri('');
 $client->setScopes(array(
 'https://www.googleapis.com/auth/calendar',
 'https://www.googleapis.com/auth/calendar.readonly',
 ));
 if (isset($_GET['code'])) {
 $client->authenticate($_GET['code']);
 $_SESSION['token'] = $client->getAccessToken();
 if (isset($_SESSION['token'])) {
 $client->setAccessToken($_SESSION['token']);
 }
 }

 if ($client->getAccessToken()) {
    $service = new Google_Service_Calendar($client);
    $event = new Google_Service_Calendar_Event(array(
        'summary' => "Myevent",
        'location' => "31 Shuijd",
        'description' => "Test Descriptionj",
        'start' => array(
          'dateTime' => "12/1/2016",
          'timeZone' => "Asia/Kolkata",
        ),
        'end' => array(
          'dateTime' => "12/2/2016",
          'timeZone' => "Asia/Kolkata",
        ),
        'recurrence' => array(
          'RRULE:FREQ=DAILY;COUNT=2'
        ),
        'attendees' => array(
          array('email' => $row['contact_email']),
        ),
        'reminders' => array(
          'useDefault' => FALSE,
          'overrides' => array(
            array('method' => 'email', 'minutes' => 24 * 60),
            array('method' => 'popup', 'minutes' => 10),
          ),
        ),
      ));
    $event = $service->events->insert("primary", $event);


 } 
 ?>

3 个答案:

答案 0 :(得分:0)

你朝着一个好方向前进。您只需在设置访问类型offline后使用从Google收到的刷新令牌刷新访问令牌。您应该为每个授予您离线访问权限的用户保存在数据库中的刷新令牌,这样您就可以刷新访问令牌而无需再次请求权限。

代码看起来像这样:

$client->refreshToken($refreshToken);
$tokens = $client->getAccessToken();
$tokens->refresh_token = $refreshToken; // the refresh token is not returned, so if you're updating the session or database, you should set it manually so you don't lose it

答案 1 :(得分:0)

首次登录时,您只需将访问令牌存储到会话中 当你想要创建事件时传递这个

$session_token = <access token store in session >
if (!$client->getAccessToken()) {
     $client->setAccessToken($session_token);
}

编辑::更改

 if (isset($_GET['code'])) {
     $client->authenticate($_GET['code']);
     $_SESSION['token'] = $client->getAccessToken();
 }

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

答案 2 :(得分:-1)

我将google日历与我的应用程序成功集成。第一次,我成功进行身份验证,所有事件都在谷歌日历上正确创建。 - &gt; 请在首次创建活动时附上代码