Oauth - Google authentication - get new access token

Hi,

I’m desperate for finding how to request for new access_token given refresh_token. During sign up process I store access_token and refresh_token.

now, I would like to know how to check if the token has expired. If so, get a new access_token using refresh_token. I couldn’t find any useful information.

I’m happy to pay someone for this services.

hi,

I highly suggest you use one of the existing authclient which takes care of all of this rather than reinventing the wheel, yii provides very good auth client to take care of this also you can also use other auth services without changing a lot of code.

here is a link to the docs

http://www.yiiframework.com/doc-2.0/ext-authclient-index.html

Thank you for the reply.

I’ve already using http://www.yiiframework.com/doc-2.0/yii-authclient-clients-google.html.

There are no examples of using refresh_token to request new access_token. Google’s access_token expires in 3600secs.

Even you google it you will not find. I can sign and save tokens. But using refresh_token I can’t get access token.

Thank you

ok if you are using the yii2-authclient then yii should take care of it unless you have customized it, where are you storing the tokens?

this is file that handles the logic to get access_token and set access_token

https://github.com/yiisoft/yii2-authclient/blob/master/BaseOAuth.php

line #106 has a method declaration for getAccessToken() method which calls restoreAccessToken() defined on line #260 look at the body of that method, it will check if token is expired if it is it will get a new one


    /**

     * Restores access token.

     * @return OAuthToken auth token.

     */

    protected function restoreAccessToken()

    {

        $token = $this->getState('token');

        if (is_object($token)) {

            /* @var $token OAuthToken */

            if ($token->getIsExpired() && $this->autoRefreshAccessToken) {

                $token = $this->refreshAccessToken($token);

            }

        }

        return $token;

    }