【问题标题】:Google oauth 2.0 - do i need refresh token?Google oauth 2.0 - 我需要刷新令牌吗?
【发布时间】:2020-05-15 14:42:04
【问题描述】:

在我的网页上,我有简单的登录系统 (PHP)。您提供邮件和密码,系统创建 _SESSION 并登录。

我想添加使用 google oAuth 2.0 登录的选项。

我创建了 index.php 文件:

require_once("vendor/autoload.php");
$client = new Google_Client();
$client->addScope(array('https://www.googleapis.com/auth/userinfo.email','https://www.googleapis.com/auth/userinfo.profile'));
$redirect_uri = '/login.php';
$client->setRedirectUri($redirect_uri);

<a href="<?=$client->createAuthUrl()?>">LOGIN WITH GOOGLE</a>

用户将点击链接,在选择一个谷歌帐户后,他将被重定向到:

login.php 文件:

if(isset($_GET['code'])) {
    try {
        $token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
    }catch (Exception $e){
        //redirect to login.php - invalid login
        exit();
    }

    try {
        $pay_load = $client->verifyIdToken();
    }catch (Exception $e) {
        //redirect to login.php - invalid login
        exit();
    }
    //NOW I KNOW USER IS VALID,
    //create my own _SESSION and redirect to index.php as logged user (i will use mail and name of user from google). 
    // i am managing _SESSION so if user logOut i will just destroy session, or will be destroyed after few minutes of inactivity
}

这种方法好吗?

我需要使用吗

setAccessToken($token);
verifyIdToken();

谢谢你的回答。

【问题讨论】:

    标签: php google-oauth


    【解决方案1】:

    我认为您不了解 Authorizationauthentication 之间的区别。

    Oauth2 grants you authorization to access a users data is denoted by an access token and a refresh token. 刷新令牌允许您在用户离线时通过请求新的访问令牌来访问用户数据。使用 Oauth2 并不能保证调用背后有用户。

    OpenId connect Authenticates is a user logging in and authenticating using a login and password or google login that this is the user. 这由 Id 令牌表示。登录后总会有实际用户打开 id connect。

    在我的网页上,我有简单的登录系统 (PHP)。您提供邮件和密码,系统创建 _SESSION 并登录。

    如果您使用的是登录系统,那么您应该使用 openid connect,它在用户离线时不需要刷新令牌来访问用户数据。用户将始终存在于登录系统中。

    【讨论】:

    • 所以在$token = $client-&gt;fetchAccessTokenWithAuthCode($_GET['code']); 之后我添加$client-&gt;setAccessToken($token['access_token']); 然后我可以使用函数$google_data = new Google_Service_Oauth2($client); 来获取用户数据。对吗?
    【解决方案2】:

    如果这里的主要问题是“我需要刷新令牌吗?”答案是......访问令牌将过期。到期后,您将有两种选择:

    1. 让用户再次完成登录过程。
    2. 使用刷新令牌获取新的访问令牌。

    刷新令牌的唯一目的是在过期后获得新的访问令牌,而无需将用户重定向到谷歌并再次返回。如果您不介意这样做,或者您只需要在短时间内访问他们的帐户,那么您实际上不需要使用刷新令牌。

    【讨论】:

    • 感谢您的回答。我只需要 20 分钟,oAuth 中的过期时间我认为是 60 就足够了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-27
    • 1970-01-01
    • 2015-11-04
    • 2014-09-22
    • 2015-05-17
    • 2015-04-01
    • 2014-01-20
    相关资源
    最近更新 更多