B
B
bekendorf2016-10-02 12:21:32
Google Calendar
bekendorf, 2016-10-02 12:21:32

google api token refresh?

Good afternoon. I'm trying to deal with the google api, I started with the calendar. Everything works until the moment when you need to update the token, then there is an error:

Fatal error: Uncaught exception 'Google_Service_Exception' with message '{ "error": { "errors": [ { "domain": "global", "reason": "authError", "message": "Invalid Credentials", "locationType ": "header", "location": "Authorization" } ], "code": 401, "message": "Invalid Credentials" } }

The code:
require_once 'google-php-api/vendor/autoload.php';

    session_start();

    $client = new Google_Client();
    $client->addScope(Google_Service_Calendar::CALENDAR);
    $client->setAccessType('offline');
    $client->setApprovalPrompt('force'); 
    $client->setIncludeGrantedScopes(true);

    $refreshToken= $_SESSION['access_token']['refresh_token'];

    if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
      if ($client->isAccessTokenExpired()) {
            $client->refreshToken($refreshToken);
            $newAccessToken = $client->getAccessToken();
            $newAccessToken['refresh_token'] = $refreshToken;
            $client->getAccessToken($newAccessToken);
            $client->setAccessToken($newAccessToken);
            $_SESSION['access_token'] = $newAccessToken;
      }

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

      define("SCOPE",Google_Service_Calendar::CALENDAR);
      define("APP_NAME","My Project");
      $application_creds = 'My Project*****.json';
      $credentials_file = file_exists($application_creds) ? $application_creds : false;
      $client->setAuthConfig($credentials_file);
$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-09-25T10:00:00.000',
        'timeZone' => 'GMT +03:00',
    ),
    'end' => array(
        'dateTime' => '2016-09-25T11:00:00.000',
        'timeZone' => 'GMT +03:00',
    ),
    'attendees' => array(
        array(
            'email' => '[email protected]',
            'organizer' => true
            ),
        array(
            'email' => '[email protected]',
            'resource' => true
        ),
    ),
    "creator"=> array(
        "email" => "[email protected]",
        "displayName" => "Example",
        "self"=> true
    ),
    "guestsCanInviteOthers" => false,
    "guestsCanModify" => false,
    "guestsCanSeeOtherGuests" => false,
));

    $service = new Google_Service_Calendar($client);
    $calendarId = 'primary';
    $event1 = $service->events->insert($calendarId, $event);
    printf('Event created: %s\n', $event1->htmlLink);
    echo "<br>";
    echo $event1->id;

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

Where am I wrong?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question