如何获取刷新令牌以使用google api登录?

问题描述 投票:0回答:2

首先,我是 google api 的绝对初学者。我遵循了本教程以及网络上的许多其他教程 ->

https://www.webslesson.info/2019/09/how-to-make-login-with-google-account-using-php.html
并且它正在成功运行。但登录后我无法刷新页面,因为我没有刷新令牌。

我的config.php-

<?php

session_start();
require_once 'vendor/autoload.php';
$google_client = new Google_Client();
$google_client->setClientId('client id here');
$google_client->setClientSecret('client screct here');
$google_client->setRedirectUri('http://localhost/realestate/index.php');
$google_client->addScope('email');
$google_client->addScope('profile');

$google_client->setAccessType('offline');
$google_client->setApprovalPrompt('force');

?>

我的索引文件-

<?php

//Include Configuration File
include('config.php');

$login_button = '';


if(isset($_GET["code"]))
{

$token = $google_client->fetchAccessTokenWithAuthCode($_GET["code"]);

if(!isset($token['error']))
{

$google_client->setAccessToken($token['access_token']);

$_SESSION['access_token'] = $token['access_token'];

$google_service = new Google_Service_Oauth2($google_client);


$data = $google_service->userinfo->get();


    if(!empty($data['given_name']))
    {
    $_SESSION['user_first_name'] = $data['given_name'];
    }

    if(!empty($data['family_name']))
    {
    $_SESSION['user_last_name'] = $data['family_name'];
    }

    if(!empty($data['email']))
    {
    $_SESSION['user_email_address'] = $data['email'];
    }

        if(!empty($data['gender']))
        {
        $_SESSION['user_gender'] = $data['gender'];
        }

        if(!empty($data['picture']))
        {
            $_SESSION['user_image'] = $data['picture'];
        }
  

    }
}


if(!isset($_SESSION['access_token']))
{

    $login_button = '<a href="'.$google_client->createAuthUrl().'">Login With Google</a>';
}

?>

我问了一个与这个问题有些相关的问题,但没有用。所以我在这里再次询问,抱歉。

那么,我该如何设置刷新令牌?

php google-oauth google-api-php-client
2个回答
1
投票

您实际上确实有一个刷新令牌。这个问题或者更确切地说谷歌认为你这样做。

当您将其添加到代码中时

$google_client->setAccessType('offline');

它告诉谷歌您想在用户不在线时访问用户数据。 然而,您遇到的问题是,对于某些语言(例如 PHP),Google 在用户第一次进行身份验证时会向您发送刷新令牌,然后他们假设您已将其存储在服务器上的某个位置,以便您下次可以使用它时间。如果您再次登录用户,您通常不会被授予新的刷新令牌,因为他们再次认为您拥有一个。

解决此问题的方法是让用户撤销您的访问权限撤销。然后,下次他们访问您的网站时,系统应该提示他们登录,这次您应该有一个刷新令牌。记得把它存放在某个地方,以便下次使用。

此外,当您刷新访问令牌时,请记住检查刷新令牌是否存在,如果它与您拥有的不同,则存储新的令牌

// Refresh the token if it's expired.
    if ($client->isAccessTokenExpired()) {
        $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
        file_put_contents($credentialsPath, json_encode($client->getAccessToken()));
    }

为沮丧的人更新。用户447951

如果即使在离线添加后您也没有看到刷新令牌,这是因为此客户端库在每次调用、授权请求或刷新访问令牌后都不会向您返回刷新令牌。 (是的,一些谷歌客户端库可以,但这个不行)

事实并非如此,因为假设用户第一次授权应用程序时,开发人员会存储它,因此下次授权用户时,您将不会获得新的刷新令牌。当您刷新访问令牌时也不会得到一个。

您需要转到用户的谷歌帐户并撤销第三方应用程序的访问权限,然后再次请求用户授权。或使用撤销端点撤销用户访问权限。当再次提示用户请求授权时,将返回新的刷新令牌。这次存起来吧。

不,仅删除系统中存储的凭据并使用强制提示是不够的。用户帐户中不得有任何内容表明他们之前已授予此应用程序访问权限。

  1. 使用撤销端点撤销用户对应用程序的访问权限
  2. 转到具有访问权限的第三方通行证下的用户帐户并将其撤销。

0
投票

当您在代码中调用此方法时:

$token = $google_client->fetchAccessTokenWithAuthCode($_GET["code"]);

您也将在

$token['refresh_token']
中获得刷新令牌; 将此刷新令牌保存在您的一端并像这样使用它

$client->fetchAccessTokenWithRefreshToken($token['refresh_token']);
© www.soinside.com 2019 - 2024. All rights reserved.