Search code examples
phpgoogle-apigoogle-calendar-apigoogle-oauthgoogle-api-php-client

How to reload a users authorization


I am using the Google Calendar API.

I authenticate the user on the index page to log the $_GET['code'], I have to do it at the index because I need get variables to add an event from php. I have the impression that the token is changing and causing a bug (which I can avoid by disconnecting and reconnecting).

Here is the connection to google on my index page:

$client = new Google_Client();
$client->setAuthConfig('client_secret.json');
$client->addScope(Google_Service_Calendar::CALENDAR);
$client->setRedirectUri($monsite);
$client->setAccessType('offline');
$client->setIncludeGrantedScopes(true);
if (isset($_GET['code'])) {
    $client->authenticate($_GET['code']);
    $_SESSION['oauth'] = $client->getAccessToken();
}
if (!isset($_SESSION['oauth'])) {
    $auth_url = $client->createAuthUrl();
    header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
    die();
}

Also, the event I am creating has a creator and guests. It's not quite what I'm looking for, I would rather insert an event on a calendar without asking if the person is interested (because they are).

Here is the event part of my PHP page:

$event = new Google_Service_Calendar_Event(array(
    'summary' => $client. " / " .$contrat,
    'location' => $adresse,
    'description' => $desc,
    'start' => array(
      'dateTime' => $start,
      'timeZone' => 'Europe/Paris',
    ),
    'end' => array(
      'dateTime' => $end,
      'timeZone' => 'Europe/Paris',
    ),
    'attendees' => $invites,
));

I therefore wish to no longer have guests but "creators" who will be informed in advance.

So to recap I have two questions:

1- How to keep / reload user authentication.

2- How to turn events into "tasks".

I'm not sure I was very clear and I'm probably asking a lot, but it could be of great help to me.

In any case, thank you in advance for your answers! :)


Solution

  • You probably auto refreshing token using setTokenCallback(); might help you. Here is the usaqe : https://github.com/googleapis/google-api-php-client/issues/1758

    token parsed by cache need to be cleared, why ? The client will cache access tokens for calls to the same service with the same scopes. To change tokens,

    $client->setAccessToken($token1);
    // <call 1>
    
    $client->getCache()->clear();
    
    $client->setAccessToken($token2);
    // <call 2>
    

    Here is the issue : https://github.com/googleapis/google-api-php-client/issues/1672

    And here is why change token :Why caching access token is consider bad in oauth2?