【发布时间】:2021-03-20 00:49:15
【问题描述】:
我正在使用生成访问令牌并且它工作成功,因为访问令牌的时间很短,只有 1 小时,我想获取用户的刷新令牌并存储在数据库中,以便我可以获得访问权限任何时候我需要的令牌。
下面是我在两个文件中的代码。
文件 Oauth.php
<?php
require 'vendor/autoload.php';
// Refer to the PHP quickstart on how to setup the environment:
$client = new Google_Client();
$client->setAccessType('offline');
$client->setAuthConfigFile('client_secret.json'); //file downloaded earlier
$client->addScope("https://www.googleapis.com/auth/calendar");
$auth_url = $client->createAuthUrl();
header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL)); //redirect user to Google
第二个文件 get_token.php
<?php
require 'vendor/autoload.php';
// Refer to the PHP quickstart on how to setup the environment:
$client->authenticate($_GET['code']);
$access_token = $client->getAccessToken();
$client->setAccessToken($access_token);
?>
提前致谢
【问题讨论】:
标签: php oauth-2.0 google-oauth