美好的一天。我正在尝试处理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));
}