duanji1902 2016-11-16 20:45
浏览 37

要求授权仅使用第一次使用日历

I wanna only ask to the user one time to access the account. I have the token but after 3600 seconds the token expires.

This is my code (working) "inside" a framework. Someone can tell how can i do it?

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']);
    }
}

Inside of session i have:

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

Thank you

  • 写回答

1条回答 默认 最新

  • dongxing1965 2016-11-17 03:21
    关注

    The very first time your application is authenticating the user, you are returned a refresh token inside $_SESSION['access_token'] = $client->getAccessToken();. That is when you need to get the refresh token $refrshToken = $_SESSION['access_token']['refresh_token]; so you can use it after 3600 seconds to refresh the access token. If you did not save the refresh token the first time your app authenticated, then you have to set the approval prompt to force $client->setApprovalPrompt('force');, or alternatively you can delete the app from your connected apps and sites https://security.google.com/settings/security/permissions?utm_source=OGB

    You can try modifying your actionCallback function like this

    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']);
    
        }
    }
    

    And then try modifying your actionEvent function like this

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

    Just PLEASE BARE IN MIND THAT SAVING THE REFRESH TOKEN TO A COOKIE IS NOT RECOMMENDED! This is only so you can test how the refresh token system works. I would strongly recommend you to save it to a file or to a sql database. I hope this helps!

    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示