Добрый день. Пытаюсь разобраться с google api, начал с календаря. Все работает до момента, когда надо обновить токен, далее идет ошибка:
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" } }
Код:
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' => 'mail@gmail.com',
'organizer' => true
),
array(
'email' => 'tobias@gmail.com',
'resource' => true
),
),
"creator"=> array(
"email" => "email@example.com",
"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));
}
В чем я ошибаюсь?