Google oauth2访问令牌在1小时内过期。我想把它做成1天 [英] Google oauth2 access token expires in 1 hour. I want to make it 1 day

查看:163
本文介绍了Google oauth2访问令牌在1小时内过期。我想把它做成1天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个项目,其中包含Google OAuth2 凭据,可用于Google日历。然而,访问权限每1小时到期。

有人可以帮助我将过期时间更改为1天。



I使用此代码来访问谷歌日历事件:

  $ 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();


解决方案

了解Oauth2。访问令牌是短暂的,它们只能持续一个小时,这是他们工作的方式,你不能改变它。



你应该做的是存储返回的刷新令牌通过身份验证过程设置以下内容。

  $ client-> setAccessType('offline'); 

通过使用刷新令牌,您可以请求新的访问令牌。这个例子可能会帮助它看起来如何在访问令牌过期时设置访问令牌。 上传示例

可能是沿着这条线的东西。

  $ client = new Google_Client(); 
$ client-> setApplicationName(APPNAME);
$ client-> setClientId(CLIENTID); // client id
$ client-> setClientSecret(CLIENTSECRET); //客户端密码
$ client-> setRedirectUri(REDIRECT_URI); // redirect uri
$ client-> setApprovalPrompt('auto');

$ client-> setAccessType('offline'); //产生刷新令牌

$ token = $ _COOKIE ['ACCESSTOKEN'];

//如果cookie中存在token
if($ token){
//使用相同的令牌
$ client-> setAccessToken($ token) ;
}

//如果cookie标记不存在,则此行获取新标记
//否则,相同的cookie标记
$ token = $ client- > getAccessToken();

if($ client-> isAccessTokenExpired()){//如果令牌过期
$ refreshToken = json_decode($ token) - > refresh_token;

//刷新令牌
$ client-> refreshToken($ refreshToken);
}

return $ client;
}


I have created a project with Google OAuth2 credentials for use with Google calendar.

However the access toke expires in every 1 hour.

Can someone please help me change the expire time to 1 day.

I have use this code to access the google calendar events:

$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();
}

解决方案

There are some things you need to understand about Oauth2. Access tokens are short lived they only last for one hour this is the way they work and you can not change that.

What you should be doing is storing the refresh token returned by the authentication process when you set the following.

$client->setAccessType('offline');

By using the refresh token you can request a new access token. This example might help it appears to show how to set the access token when it has expired. upload example

probably something along this lines.

    $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;
}

这篇关于Google oauth2访问令牌在1小时内过期。我想把它做成1天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆