如何使用刷新令牌(php)获取google api的更新访问令牌?

发布于 2025-01-10 02:13:01 字数 3391 浏览 4 评论 0原文

为了使代码正常工作,使用 Google 的官方库通过 PHP 来使用 API。

// connect the library to work with the Google API
require_once 'vendor/autoload.php';
echo '<pre>';

在此示例中,所有键都是手动设置的。

$array = '{"access_token":"***","expires_in":3599,"scope":"https:\/\/www.googleapis.com\/auth\/calendar https:\/\/www.googleapis.com\/auth\/userinfo.profile openid https:\/\/www.googleapis.com\/auth\/userinfo.email","token_type":"Bearer","id_token":"****","created":1644878660,"refresh_token":"**************"}';
print_r($array);

// Application Options
// Application ID
define('GOOGLE_CLIENT_ID', '************');
// Secret Apps
define('GOOGLE_CLIENT_SECRET', '*********************');
// The address to which the user will be redirected after authorization
define('GOOGLE_CLIENT_REDIRECT_URL', 'https://*************/api/google-login.php');

需要访问的区域。

// Areas to which access will be requested
define('SCOPES', implode(' ', [
    Google_Service_Calendar::CALENDAR,
    'https://www.googleapis.com/auth/userinfo.profile',
    'https://www.googleapis.com/auth/userinfo.email'
]));

// Initializing the Google Client
$client = new Google_Client();
$client->setClientId(GOOGLE_CLIENT_ID);
$client->setClientSecret(GOOGLE_CLIENT_SECRET);
$client->setRedirectUri(GOOGLE_CLIENT_REDIRECT_URL);
$client->setScopes(SCOPES);
$client->setAccessType('offline');
$client->setApprovalPrompt('force');

// Retrieving a token from an array
$accessToken = json_decode($array, true);
print_r($accessToken);
$client->setAccessToken($accessToken);

实际上是提出最多问题的领域。

// If the token has expired, update it
if ($client->isAccessTokenExpired()) {
 
    // When refreshing a refresh token, the token is missing
    // Therefore, we save the refresh token in a separate variable
    $refreshTokenSaved = $client->getRefreshToken();
 
    // Refreshing the token
    $client->fetchAccessTokenWithRefreshToken($refreshTokenSaved);
 
    // We create a new variable in which we place the new updated token
    $accessTokenUpdated = $client->getAccessToken();
 
    // Add the old refresh token to this variable
    $accessTokenUpdated['refresh_token'] = $refreshTokenSaved;
 
    // Installing a new token
    $client->setAccessToken($accessTokenUpdated);
    
    
    echo $refreshTokenSaved.'<br>';
    print_r($accessTokenUpdated);
    echo '<br>';
    
}

我们显示用户数据请求的结果以检查访问令牌的有效性。

if (!$client->isAccessTokenExpired()) {
    $oauth2 = new Google_Service_Oauth2($client);
    $userInfo = $oauth2->userinfo->get();
    
    echo '<br><br>';
    echo 'Email: ';
    echo $userInfo->email;
    echo '<br><br>';
    echo 'sex: ';
    echo $userInfo->gender;
    echo '<br><br>';
    echo 'name: ';
    echo $userInfo->givenName;
    echo '<br><br>';
    echo 'Surname: ';
    echo $userInfo->familyName;
    echo '<br><br>';
    echo 'Full name: ';
    echo $userInfo->name;
    echo '<br><br>';
    echo 'ID: ';
    echo $userInfo->id;
    echo '<br><br>';
    echo 'Photo link: ';
    echo $userInfo->picture;
    echo '<br><br>';
} else {
    echo 'Error!';
}

echo '</pre>';

请检查我的代码。现在它正在返回旧的(无效的)访问令牌。谢谢!

For the code to work, the official library from Google is used to work with the API through PHP.

// connect the library to work with the Google API
require_once 'vendor/autoload.php';
echo '<pre>';

In this example, all keys are set manually.

$array = '{"access_token":"***","expires_in":3599,"scope":"https:\/\/www.googleapis.com\/auth\/calendar https:\/\/www.googleapis.com\/auth\/userinfo.profile openid https:\/\/www.googleapis.com\/auth\/userinfo.email","token_type":"Bearer","id_token":"****","created":1644878660,"refresh_token":"**************"}';
print_r($array);

// Application Options
// Application ID
define('GOOGLE_CLIENT_ID', '************');
// Secret Apps
define('GOOGLE_CLIENT_SECRET', '*********************');
// The address to which the user will be redirected after authorization
define('GOOGLE_CLIENT_REDIRECT_URL', 'https://*************/api/google-login.php');

Areas to which access will be requested.

// Areas to which access will be requested
define('SCOPES', implode(' ', [
    Google_Service_Calendar::CALENDAR,
    'https://www.googleapis.com/auth/userinfo.profile',
    'https://www.googleapis.com/auth/userinfo.email'
]));

// Initializing the Google Client
$client = new Google_Client();
$client->setClientId(GOOGLE_CLIENT_ID);
$client->setClientSecret(GOOGLE_CLIENT_SECRET);
$client->setRedirectUri(GOOGLE_CLIENT_REDIRECT_URL);
$client->setScopes(SCOPES);
$client->setAccessType('offline');
$client->setApprovalPrompt('force');

// Retrieving a token from an array
$accessToken = json_decode($array, true);
print_r($accessToken);
$client->setAccessToken($accessToken);

Actually the area that raises the most questions.

// If the token has expired, update it
if ($client->isAccessTokenExpired()) {
 
    // When refreshing a refresh token, the token is missing
    // Therefore, we save the refresh token in a separate variable
    $refreshTokenSaved = $client->getRefreshToken();
 
    // Refreshing the token
    $client->fetchAccessTokenWithRefreshToken($refreshTokenSaved);
 
    // We create a new variable in which we place the new updated token
    $accessTokenUpdated = $client->getAccessToken();
 
    // Add the old refresh token to this variable
    $accessTokenUpdated['refresh_token'] = $refreshTokenSaved;
 
    // Installing a new token
    $client->setAccessToken($accessTokenUpdated);
    
    
    echo $refreshTokenSaved.'<br>';
    print_r($accessTokenUpdated);
    echo '<br>';
    
}

We display the result of the request for user data to check the validity of the access token.

if (!$client->isAccessTokenExpired()) {
    $oauth2 = new Google_Service_Oauth2($client);
    $userInfo = $oauth2->userinfo->get();
    
    echo '<br><br>';
    echo 'Email: ';
    echo $userInfo->email;
    echo '<br><br>';
    echo 'sex: ';
    echo $userInfo->gender;
    echo '<br><br>';
    echo 'name: ';
    echo $userInfo->givenName;
    echo '<br><br>';
    echo 'Surname: ';
    echo $userInfo->familyName;
    echo '<br><br>';
    echo 'Full name: ';
    echo $userInfo->name;
    echo '<br><br>';
    echo 'ID: ';
    echo $userInfo->id;
    echo '<br><br>';
    echo 'Photo link: ';
    echo $userInfo->picture;
    echo '<br><br>';
} else {
    echo 'Error!';
}

echo '</pre>';

Please check my code. Right now it's returning the old (invalid) access token. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文