Google API calendar の quickstart.php を実行させAuth2.0でカレンダーにアクセスし、最終的にはこれを改造してイベントの読み書きをさせたいと考えております。
google api client には、
google-api-php-client-2.1.3を使用しており、
auth認証用クライアントIDを「その他」で作成し、ダウンロードしてclient_secrets.jsonとしております。
作業ディレクトリ内で、composer もインストールされており、この状態で php quickstart.php を実行しました。(https://developers.google.com/google-apps/calendar/quickstart/php)
実行したコードは以下の通り。
<?php
require_once __DIR__ . '/google-api-php-client-2.1.3/src/Google/autoload.php';
define('APPLICATION_NAME', 'Google Calendar API PHP Quickstart');
define('CLIENT_SECRET_PATH',__DIR__ . 'client_secrets.json');
// If modifying these scopes, delete your previously saved credentials
// at ~/.credentials/calendar-php-quickstart.json
define('SCOPES', implode(' ', array(
Google_Service_Calendar::CALENDAR)
));
if (php_sapi_name() != 'cli') {
throw new Exception('This application must be run on the command line.');
}
/**
* 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->setAuthConfig(CLIENT_SECRET_PATH);
$client->setAccessType('offline');
// Load previously authorized credentials from a file.
$credentialsPath = expandHomeDirectory(CREDENTIALS_PATH);
if (file_exists($credentialsPath)) {
$accessToken = json_decode(file_get_contents($credentialsPath), true);
} 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->fetchAccessTokenWithAuthCode($authCode);
// Store the credentials to disk.
if(!file_exists(dirname($credentialsPath))) {
mkdir(dirname($credentialsPath), 0700, true);
}
file_put_contents($credentialsPath, json_encode($accessToken));
printf("Credentials saved to %s\n", $credentialsPath);
}
$client->setAccessToken($accessToken);
// Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
file_put_contents($credentialsPath, json_encode($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);
}
// Get the API client and construct the service object.
$client = getClient();
$service = new Google_Service_Calendar($client);
// Print the next 10 events on the user's calendar.
$calendarId = '[email protected]';
$optParams = array(
'maxResults' => 10,
'orderBy' => 'startTime',
'singleEvents' => TRUE,
'timeMin' => date('c'),
);
$results = $service->events->listEvents($calendarId, $optParams);
if (count($results->getItems()) == 0) {
print "No upcoming events found.\n";
} else {
print "Upcoming events:\n";
foreach ($results->getItems() as $event) {
$start = $event->start->dateTime;
if (empty($start)) {
$start = $event->start->date;
}
printf("%s (%s)\n", $event->getSummary(), $start);
}
}
しかし以下のエラーとなりました。
php quickstart.php
PHP Fatal error: Uncaught exception 'InvalidArgumentException' with message 'file does not exist' in /var/www/html/google-api-php-client-2.1.3/src/Google/Client.php:849
Stack trace:
#0 /var/www/html/quickstart.php(25): Google_Client-
>setAuthConfig('/var/www/html/n...')
#1 /var/www/html/quickstart.php(73): getClient()
#2 {main}
thrown in /var/www/html/google-api-php-client-2.1.3/src/Google/Client.php on line 849
このエラーの解決を探しましたが、解決策がみつかりません。
見落としている点がありましたらご教授ください。
参考URL
https://github.com/google/google-api-php-client/issues/988
https://github.com/google/google-api-php-client/issues/1055
require_once __DIR__ . '/google-api-php-client-2.1.3/src/Google/autoload.php';
のsrc/Googleを vendor に変えてrequire_once __DIR__ . '/google-api-php-client-2.1.3/vendor/autoload.php';
にしてみましたが、フリーズしました