3 php google-calendar-api google-api google-oauth google-api-php-client
我已经创建了一个具有Google OAuth2凭据的项目,可用于Google日历。
但是访问令牌每隔1小时就会过期。
有人可以帮我将过期时间更改为1天。
我已使用此代码访问Google日历事件:
$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->setAccessType('offline');
$client->setScopes(array('https://www.googleapis.com/auth/calendar'));
if (isset($_GET['code']))
$google_oauth_code = $_GET['code'];
$client->authenticate($_GET['code']);
$_SESSION['token'] = $client->getAccessToken();
$_SESSION['last_action'] = time();
}
Run Code Online (Sandbox Code Playgroud)
关于Oauth2,您需要了解一些事情。访问令牌是短暂的,它们只能持续一小时,这是它们的工作方式,您无法更改。
设置以下内容时,您应该做的是存储身份验证过程返回的刷新令牌。
$client->setAccessType('offline');
Run Code Online (Sandbox Code Playgroud)
通过使用刷新令牌,您可以请求一个新的访问令牌。该示例可能会有助于显示过期时如何设置访问令牌。 上传范例
可能与此类似。
$client = new Google_Client();
$client->setApplicationName(APPNAME);
$client->setClientId(CLIENTID); // client id
$client->setClientSecret(CLIENTSECRET); // client secret
$client->setRedirectUri(REDIRECT_URI); // redirect uri
$client->setApprovalPrompt('auto');
$client->setAccessType('offline'); // generates refresh token
$token = $_COOKIE['ACCESSTOKEN'];
// if token is present in cookie
if($token){
// use the same token
$client->setAccessToken($token);
}
// this line gets the new token if the cookie token was not present
// otherwise, the same cookie token
$token = $client->getAccessToken();
if($client->isAccessTokenExpired()){ // if token expired
$refreshToken = json_decode($token)->refresh_token;
// refresh the token
$client->refreshToken($refreshToken);
}
return $client;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2690 次 |
| 最近记录: |