谷歌api授权很长一段时间

I'm trying to make some script, that will automatically add an event from Joomla add new page to a Google calendar. I made it with this code and it even works, but only for a small amount of time.

I maybe misunderstood, how the Google authorization works. First i must authorize the script to use that calendar and so on. I made it once but after an hour i had to do it again. I would like to authorize it forever or at least for one year (something longer than one hour). And i can't even authorize it during the saving process, because its in saving script, that automatically redirect back to post management.

One more thing to mention: there is an redirect uri and i set it on index.php of administration pages. Not sure if its a problem, but it shouldn't because news are added at index.php?...... so it's still the same page.

But it all works for an hour (or something around that) so i believe that problem is not redirect uri. THX

here is my code (its copy pasted from some google api example page):

        require_once "google-api-php-client/src/Google_Client.php";
        require_once "google-api-phpclient/src/contrib/Google_CalendarService.php";

        session_start();

        $client = new Google_Client();
        $client->setApplicationName("Add Google Calendar Entries");

        $client->setClientId('####');
        $client->setClientSecret('####');
        $client->setRedirectUri('####');
        $client->setDeveloperKey('####');
        $cal = new Google_CalendarService($client);

        if (isset($_GET['logout'])) {
          unset($_SESSION['token']);
        }

        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()) {
          $calList = $cal->calendarList->listCalendarList();

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

        $event = new Google_Event();
        // some calendar adding stuff

        } else {
          $authUrl = $client->createAuthUrl();
          print "<a class='login' href='$authUrl'>Authorize!</a>";
        }

        // Google Calendar API END
    }

Just to summarize: I want my authorization to work longer and maybe there is a mistake in my code or in my understanding of how that works.

The key piece to get long term access if to request "offline" access from the user when you request authorization.

$client->setAccessType('offline');
$authUrl = $client->createAuthUrl();

This should return a refresh token as well as the current session token. You MUST save this refresh token. Even if they authorize the app again later, the refresh token will NOT be sent again. (And on that note, if they are already authorized and you have them authorize again after adding offline access, sometimes you won't get the refresh token.)

Then you can do a check like the following to keep them logged in:

$client->setAccessToken($site->token);

if($client->isAccessTokenExpired()) {
    $NewAccessToken = json_decode($client->getAccessToken());
    $client->refreshToken($NewAccessToken->refresh_token);
}

Finally, I should add that you will need to save the accessToken to the database instead of just the session to actually make it persist over different sessions. Hopefully that was obvious already.