Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8584593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:48:38+00:00 2026-06-11T21:48:38+00:00

I am using the google Calendar API. This is what I want, once you

  • 0

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;

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T21:48:40+00:00Added an answer on June 11, 2026 at 9:48 pm

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to make Task in Google Calendar using Google Calendar API : c#.
I want to create Task in Google Calendar using Google Calendar API.Using C#. Looking
Well am using eclipse to make an android app which accesses google calendar API
Actually I need to create an event in Google Calendar using my app. Every
I'm trying to get a session token to use the Google Calendar API using
I'm developing an Android APP that connects to Google Calendar using GData API for
I'm new to using the Google Calendar API. I can create .ics calendars using
Using the Google Calendar API , I can successfully add , update and delete
How to access google calendar with new api on android ? Using GData APIs
I am using zendgdata library for google calendar. I am getting this error while

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.