Hi I have some problems with the Oauth by inserting a testevent into my
Google-Calender. I changed the script for reading from my calendar a
little bit and was trying the insert.
The old Google-login with email was disabled from Google some time ago
and the only way to use the Api is via Oauth.
I become the following errors running the script from the console.
php /var/www/localhost/htdocs/calendar_insert.php
PHP Fatal error: Uncaught exception 'Google_Auth_Exception' with
message 'Error refreshing the OAuth2 token, message: '{
"error" : "invalid_request",
"error_description" : "Missing required parameter: refresh_token"
}'' in /root/vendor/google/apiclient/src/Google/Auth/OAuth2.php:357
Stack trace:
#0 /root/vendor/google/apiclient/src/Google/Auth/OAuth2.php(272):
Google_Auth_OAuth2->refreshTokenRequest(Array)
#1 /root/vendor/google/apiclient/src/Google/Client.php(454):
Google_Auth_OAuth2->refreshToken(NULL)
#2 /var/www/localhost/htdocs/calendar_insert.php(49):
Google_Client->refreshToken(NULL)
#3 /var/www/localhost/htdocs/calendar_insert.php(73): getClient()
#4 {main}
thrown in /root/vendor/google/apiclient/src/Google/Auth/OAuth2.php on
line 357
Fatal error: Uncaught exception 'Google_Auth_Exception' with message
'Error refreshing the OAuth2 token, message: '{
"error" : "invalid_request",
"error_description" : "Missing required parameter: refresh_token"
}'' in /root/vendor/google/apiclient/src/Google/Auth/OAuth2.php:357
Stack trace:
#0 /root/vendor/google/apiclient/src/Google/Auth/OAuth2.php(272):
Google_Auth_OAuth2->refreshTokenRequest(Array)
#1 /root/vendor/google/apiclient/src/Google/Client.php(454):
Google_Auth_OAuth2->refreshToken(NULL)
#2 /var/www/localhost/htdocs/calendar_insert.php(49):
Google_Client->refreshToken(NULL)
#3 /var/www/localhost/htdocs/calendar_insert.php(73): getClient()
#4 {main}
thrown in /root/vendor/google/apiclient/src/Google/Auth/OAuth2.php on
line 357
I'm using the Google-php-api-client that works fine in the script for
reading from the calendar. Here the script that should insert a Testevent:
<?php
require 'vendor/autoload.php';
define('APPLICATION_NAME', 'owncrminsert');
define('CREDENTIALS_PATH', '
.credentials/.credentials/calendar-api-calenderinsert.json');
define('CLIENT_SECRET_PATH','/var/www/localhost/htdocs/vendor/client_secret_889445840794-01897crinjia9q8q2c4ta02ksd8bsftg.apps.googleusercontent.com.json');
define('SCOPES', implode(' ', array(
Google_Service_Calendar::CALENDAR)
));
/**
* Returns an authorized API client.
* @return Google_Client the authorized client object
*/
function getClient() {
$client = new Google_Client();
$client->setApplicationName(APPLICATION_NAME);
$client->setScopes(SCOPES);
$client->setAuthConfigFile(CLIENT_SECRET_PATH);
$client->setAccessType('offline');
// Load previously authorized credentials from a file.
$credentialsPath = expandHomeDirectory(CREDENTIALS_PATH);
if (file_exists($credentialsPath)) {
$accessToken = file_get_contents($credentialsPath);
} else {
// Request authorization from the user.
$authUrl = $client->createAuthUrl();
printf("Open the following link in your browser:\n%s\n", $authUrl);
print 'Enter verification code: ';
$authCode = trim(fgets(STDIN));
// Exchange authorization code for an access token.
$accessToken = $client->authenticate($authCode);
// Store the credentials to disk.
if(!file_exists(dirname($credentialsPath))) {
mkdir(dirname($credentialsPath), 0700, true);
}
file_put_contents($credentialsPath, $accessToken);
printf("Credentials saved to %s\n", $credentialsPath);
}
$client->setAccessToken($accessToken);
// Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {
$client->refreshToken($client->getRefreshToken());
file_put_contents($credentialsPath, $client->getAccessToken());
}
return $client;
}
/**
* Expands the home directory alias '~' to the full path.
* @param string $path the path to expand.
* @return string the expanded path.
*/
function expandHomeDirectory($path) {
$homeDirectory = getenv('HOME');
if (empty($homeDirectory)) {
$homeDirectory = getenv("HOMEDRIVE") . getenv("HOMEPATH");
}
return str_replace('~', realpath($homeDirectory), $path);
}
date_default_timezone_set('Europe/Berlin');
// Get the API client and construct the service object.
$client = getClient();
$service = 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' => '2015-05-28T09:00:00-07:00',
'timeZone' => 'America/Los_Angeles',
),
'end' => array(
'dateTime' => '2015-05-28T17:00:00-07:00',
'timeZone' => 'America/Los_Angeles',
),
'recurrence' => array(
'RRULE:FREQ=DAILY;COUNT=2'
),
'attendees' => array(
array('email' => '[email protected]'),
array('email' => '[email protected]'),
),
'reminders' => array(
'useDefault' => FALSE,
'overrides' => array(
array('method' => 'email', 'minutes' => 24 * 60),
array('method' => 'popup', 'minutes' => 10),
),
),
));
*/
$event = new Google_Event();
$event->setSummary('Halloween');
$event->setLocation('The Neighbourhood');
$start = new Google_EventDateTime();
$start->setDateTime('2015-9-29T10:00:00.000-05:00');
$event->setStart($start);
$end = new Google_EventDateTime();
$end->setDateTime('2015-9-29T10:25:00.000-05:00');
$event->setEnd($end);
$calendarId = 'primary';
$event = $service->events->insert($calendarId, $event);
printf('Event created: %s\n', $event->htmlLink);
?>
Regards,
Ruprecht
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php