php - How to reload a users authorization

854

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! :)

485

Answer

Solution:

You probably auto refreshing token usingsetTokenCallback(); 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?

People are also looking for solutions to the problem: Nested Foreach() PHP

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.