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

时间:2016-11-16 20:45:51

标签: php oauth-2.0 google-calendar-api google-api-php-client google-api-client

我只想让用户一次访问该帐户。我有令牌,但在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']);
    }
}

会议内部我有:

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

谢谢

1 个答案:

答案 0 :(得分: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();
    }
}

请注意,不要推荐将新鲜的东西放到COOKIE上!这只是为了测试刷新令牌系统的工作原理。我强烈建议您将其保存到文件或SQL数据库。我希望这有帮助!