【发布时间】:2017-03-31 05:34:40
【问题描述】:
我只想要求用户访问该帐户一次。我有令牌,但 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
谢谢
【问题讨论】:
-
与您今天早些时候提出的另一个问题基本相同? stackoverflow.com/questions/40625179/…
-
是的,但现在有代码。
-
你可以更新令牌
-
但是怎么做呢?我需要将第一个令牌存储在某个地方?
-
是的,您存储第一个令牌之后,您只需使用存储的令牌来获取新令牌。您可以无限地执行此操作(直到用户删除权限)
标签: php oauth-2.0 google-calendar-api google-api-php-client google-api-client