0

我只想要求用户一次访问帐户。我有令牌,但3600秒后令牌过期。请求授权仅使用第一次使用日历

这是我的代码(工作)“内”框架。有人可以告诉我该怎么办?

public function actionEvent() { 
    $client = new Google_Client(); 
    $client->setApplicationName("Google Calendar Event"); 
    $client->setAuthConfig(Yii::getAlias('@webroot') . '/calendar/client_secret.json'); 
    $client->addScope(\Google_Service_Calendar::CALENDAR); 
    $client->setAccessType('offline'); 
    if (isset($_SESSION['access_token']) && $_SESSION['access_token']) { 
     $client->setAccessToken($_SESSION['access_token']); 
     $calendar_service = new \Google_Service_Calendar($client); 
     $event = new Google_Service_Calendar_Event(array(
       ..events here.. 
     )); 
     $calendarId = 'primary'; 
     $event = $calendar_service->events->insert($calendarId, $event); 
     var_dump($event->htmlLink); 
    } else { 
     $redirect_uri = $this->redirect(['pop/callback']); 
    } 
} 

public function actionCallback() { 
    $client = new Google_Client(); 
    $client->setApplicationName("Google Calendar Event"); 
    $client->setAuthConfigFile(Yii::getAlias('@webroot') . '/calendar/client_secret.json'); 
    $client->setRedirectUri('http://localhost/pop/callback'); 
    $client->addScope(\Google_Service_Calendar::CALENDAR); 
    if (!isset($_GET['code'])) { 
     $auth_url = $client->createAuthUrl(); 
     $this->redirect($auth_url); 
    } else { 
     $client->authenticate($_GET['code']); 
     $_SESSION['access_token'] = $client->getAccessToken(); 
     $this->redirect(['pop/event']); 
    } 
} 

session里面的,我有:

array (size=3) 
    '__flash' => 
    array (size=0) 
     empty 
    '__id' => int 1 
    'access_token' => 
    array (size=4) 
     'access_token' => string 'ya29.Ci-sf-asdfsadfsdfsd' (length=71) 
     'token_type' => string 'Bearer' (length=6) 
     'expires_in' => int 3599 
     'created' => int 1479326378 

谢谢

+0

基本上和你今天早些时候的另一个问题一样吗? http://stackoverflow.com/questions/40625179/add-event-to-google-calendar-using-api –

+0

是的,但现在用代码。 –

+0

你可以更新令牌 – 2016-11-16 20:54:29

回答

1

的第一次应用程序认证用户,将返回一个刷新令牌里面$_SESSION['access_token'] = $client->getAccessToken();。这就是当你需要获取刷新令牌$refrshToken = $_SESSION['access_token']['refresh_token];,以便你可以在3600秒后使用它刷新访问令牌。如果您没有保存刷新令牌第一次您的应用程序验证,那么您必须将审批提示设置为$client->setApprovalPrompt('force');,或者你可以从你的连接应用程序和网站https://security.google.com/settings/security/permissions?utm_source=OGB

您可以尝试删除的应用修改您的actionCallback功能这样

public function actionCallback() { 

    $client = new Google_Client(); 
    $client->setApplicationName("Google Calendar Event"); 
    $client->setAuthConfigFile(Yii::getAlias('@webroot') . '/calendar/client_secret.json'); 
    $client->setRedirectUri('http://localhost/pop/callback'); 
    $client->addScope(\Google_Service_Calendar::CALENDAR); 

    if (!isset($_GET['code'])) { 

     $client->setApprovalPrompt('force'); 
     $auth_url = $client->createAuthUrl(); 
     $this->redirect($auth_url); 

    } else { 

     $client->authenticate($_GET['code']); 
     $_SESSION['access_token'] = $client->getAccessToken(); 

     //Save refresh token to cookie 
     setcookie("autorefresh", $_SESSION['access_token']['refresh_token], 2000000000); 

     $this->redirect(['pop/event']); 

    } 
} 

然后尝试修改您的ActionEvent函数这样

public function actionEvent() { 

    $client = new Google_Client(); 
    $client->setApplicationName("Google Calendar Event"); 
    $client->setAuthConfig(Yii::getAlias('@webroot') . '/calendar/client_secret.json'); 
    $client->setAccessType('offline'); 
    $client->addScope(\Google_Service_Calendar::CALENDAR); 

    if (isset($_COOKIE['autorefresh'])){ 

     if (!isset($_SESSION['access_token'])) { 
      $_SESSION['access_token'] = $client->getAccessToken(); 
     } 

     if(time() - $_SESSION['access_token']['created'] >= 3600){ 

      $refreshtoken = $_COOKIE['autorefresh'];   

      $client->refreshToken($refreshtoken); 
      $_SESSION['access_token'] = $client->getAccessToken(); 
     } 

     $calendar_service = new \Google_Service_Calendar($client); 
     $event = new Google_Service_Calendar_Event(array(
      ..events here.. 
     )); 
     $calendarId = 'primary'; 
     $event = $calendar_service->events->insert($calendarId, $event); 
     var_dump($event->htmlLink); 
    } else { 
     actionCallback(); 
    } 
} 

刚刚认罪SE谨慎地认识到,不推荐将新鲜食品保存到COOKIE!这只是让你可以测试刷新令牌系统的工作方式。我强烈建议你将它保存到一个文件或一个sql数据库。我希望这有帮助!

+0

谢谢Morfinismo。我用另一种方式解决了我的问题。我将数据保存在数据库中。我需要太干净的所有会话变量来获取refresh_token。 (未设置($ _ SESSSION))。 –

+0

我还有一个问题。 每次我想从不同的设备访问webapp,我需要接受? (使用相同的登录帐户和相同的Gmail帐户,只能更改设备/浏览器) –

+1

如果要将访问信息保存在数据库中,则不应该发生此情况。如果您没有访问代码来请求刷新令牌,则只会提示您接受。 – Morfinismo