1

Good day. I try to understand google calendar api and I encountered a strange problem then use Oauth2. First time I normal authorization and add event, but after 1 hour (after token expired) I have 401 error (invalid credentials). I refresh the token (use refresh_token), but have 401 error again. New token tested on https://www.googleapis.com/oauth2/v1/tokeninfo and:

{
 "issued_to": "***.apps.googleusercontent.com",
 "audience": "***.apps.googleusercontent.com",
 "scope": "https://www.googleapis.com/auth/calendar",
 "expires_in": 3581,
 "access_type": "offline"
}

but I have 401 error then I try insert event.

It continues 1 day. For the next day I get the new token and it work for 1 hour again.

My test code:

require_once 'google-php-api/vendor/autoload.php';

session_start();

$client = new Google_Client();
$client->setAuthConfigFile('client_secret***.json');
$client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php');
$client->addScope(Google_Service_Calendar::CALENDAR);
$client->setAccessType('offline');
$client->setApprovalPrompt('force'); 

  if ($_GET['logout'] == "1") {
    session_unset();
  } else {

if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
    $client->setAccessToken($_SESSION['access_token']);

  if ($client->isAccessTokenExpired()) {
        $oldToken = json_decode(file_get_contents("token.json"));
        $client->refreshToken($oldToken->refresh_token);
        $newAccessToken = $client->getAccessToken();
        $newAccessToken['refresh_token'] = $refreshToken;
        $_SESSION['access_token'] = $newAccessToken;
        $client->setAccessToken($_SESSION['access_token']);
  } else {
      file_put_contents("token.json", json_encode($_SESSION['access_token']));
  }

        print_r($_SESSION['access_token']);

  $service = new Google_Service_Calendar($client);
  $event = new Google_Service_Calendar_Event(array(
    'summary' => 'Google I/O 2015',
    'location' => '800 Howard St., San Francisco, CA 94103',
    'description' => 'A chance to hear more about Google\'s developer products.',
    'start' => array(
        'dateTime' => '2016-10-06T10:13:00.000',
        'timeZone' => 'GMT +03:00',
    ),
    'end' => array(
        'dateTime' => '2016-10-06T10:15:00.000',
        'timeZone' => 'GMT +03:00',
    ),
    'attendees' => array(
        array(
            'email' => 'sbakul77@gmail.com',
            'resource' => true
        ),
    ),
    "creator"=> array(
        "email" => "email@example.com",
        "displayName" => "Example",
        "self"=> true
    ),
    "guestsCanInviteOthers" => false,
    "guestsCanModify" => false,
    "guestsCanSeeOtherGuests" => false,
));
$calendarId = '**@gmail.com';
$event = $service->events->insert($calendarId, $event);
printf('Event created: %s, Event id: %s', $event->htmlLink, $event->id);

} else {
  $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php';
  header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));

}
}

Why is this happening? And why my refreshing token is "invalid credentials"?

0 Answers0