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