Google API Oauth php permanent access

15,997

Ok, after waiting for a few days, the suggestion from Terry Seidler(comments below) made it all happen! Here is my piece of code on how to automaticly refresh the access token without autenticating each time using cookies.

(NOTICE: It's safer to save the refresh token in your database)

This is the magic (using cookies):

$client = new Google_Client();

    $client->setApplicationName("Wanda3.0 Agenda");

    $cal = new Google_CalendarService($client);

    $client->setAccessType('offline');

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

        $client->authenticate($_GET['code']);

        $_SESSION['token'] = $client->getAccessToken();

        header('Location: http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']);

    }

    //Where the magic happends
    if (isset($_SESSION['token'])) {

        //Set the new access token after authentication
        $client->setAccessToken($_SESSION['token']);

        //json decode the session token and save it in a variable as object
        $sessionToken = json_decode($_SESSION['token']);

        //Save the refresh token (object->refresh_token) into a cookie called 'token' and make last for 1 month
        $this->Cookie->write('token', $sessionToken->refresh_token, false, '1 month');
    }

    //Each time you need the access token, check if there is something saved in the cookie.
    //If $cookie is empty, you are requested to get a new acces and refresh token by authenticating.
    //If $cookie is not empty, you will tell the client to refresh the token for further use,
    // hence get a new acces token with the help of the refresh token without authenticating..
    $cookie = $this->Cookie->read('token');

    if(!empty($cookie)){
        $client->refreshToken($this->Cookie->read('token'));
    }

And thats it! IF you have any questions, feel free to leave a comment below and I will answer the best I can. Goodluck and Cheers!

Share:
15,997
hope_industries
Author by

hope_industries

Updated on June 03, 2022

Comments

  • hope_industries
    hope_industries almost 2 years

    I am using the google Calendar API. This is what I want, once you give the app the permission, I can always use the app, without the need of giving access everyday. I keep hearing that I need to save the access token or use the refresh token to do what I want to do.. Here is the thing, how do you do it? How does the code look like? I've tried saving the token in a cookie, but after an hour, the access token has expired. How do I keep the user logged in?

    PS: Please give me code examples with explanations.

    Here is my code (using CakePHP):

    $client = new Google_Client();
    
        $client->setApplicationName("Wanda3.0 Agenda");
    
        $cal = new Google_CalendarService($client);
    
        if (isset($_GET['code'])) {
    
            $client->authenticate($_GET['code']);
    
    
            $_SESSION['token'] = $client->getAccessToken();
    
    
            header('Location: http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']);
    
        }
    
        if (isset($_SESSION['token'])) { $client->setAccessToken($_SESSION['token']); }
    
    if ($client->getAccessToken()) {
            /************* Code entry *************/
    
    
        }else{
            /************* Not connected to google calendar code *************/
            $authUrl = $client->createAuthUrl();
    
            $returnArr = array('status' => 'false', 'message' => "<a class='login' href='$authUrl'>Connect Me!</a>");
    
            return $returnArr;
    
        }