Google Calendar API and php -
i have used google calendar api user authentication porocess shows error , did not knwo reason error strictly follow instruction of google developer code please give me answer. here code authentication
<?php require 'src/google/autoload.php'; define('application_name', 'google calendar api quickstart'); define('credentials_path', '~/.credentials/calendar-api-quickstart.json'); define('client_secret_path', 'client_secret.json'); define('scopes', implode(' ', array( google_service_calendar::calendar_readonly) )); /** * returns authorized api client. * @return google_client 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 authorized credentials file. $credentialspath = expandhomedirectory(credentials_path); if (file_exists($credentialspath)) { $accesstoken = file_get_contents($credentialspath); } else { // request authorization user. $authurl = $client->createauthurl(); printf("open following link in browser:\n%s\n", $authurl); print 'enter verification code: '; $authcode = trim(fgets(stdin)); // exchange authorization code access token. $accesstoken = $client->authenticate($authcode); // store credentials disk. if(!file_exists(dirname($credentialspath))) { mkdir(dirname($credentialspath), 0700, true); } file_put_contents($credentialspath, $accesstoken); printf("credentials saved %s\n", $credentialspath); } $client->setaccesstoken($accesstoken); // refresh token if it's expired. if ($client->isaccesstokenexpired()) { $client->refreshtoken($client->getrefreshtoken()); file_put_contents($credentialspath, $client->getaccesstoken()); } return $client; } /** * expands home directory alias '~' full path. * @param string $path path expand. * @return string expanded path. */ function expandhomedirectory($path) { $homedirectory = getenv('home'); if (empty($homedirectory)) { $homedirectory = getenv("homedrive") . getenv("homepath"); } return str_replace('~', realpath($homedirectory), $path); } // api client , construct service object. $client = getclient(); $service = new google_service_calendar($client); // print next 10 events on user's calendar. $calendarid = 'primary'; $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() $event) { $start = $event->start->datetime; if (empty($start)) { $start = $event->start->date; } printf("%s (%s)\n", $event->getsummary(), $start); } }
the output shows error :
fatal error: uncaught exception 'google_auth_exception' message 'invalid code' in c:\xampp\htdocs\lapi\odesk8\google_calendar\src\google\auth\oauth2.php:88 stack trace: #0 c:\xampp\htdocs\lapi\odesk8\google_calendar\src\google\client.php(128): google_auth_oauth2->authenticate('', false) #1 c:\xampp\htdocs\lapi\odesk8\google_calendar\test.php(33): google_client->authenticate('') #2 c:\xampp\htdocs\lapi\odesk8\google_calendar\test.php(66): getclient() #3 {main} thrown in c:\xampp\htdocs\lapi\odesk8\google_calendar\src\google\auth\oauth2.php on line 88
the sample code provided reads stdin ($authcode = trim(fgets(stdin));
) if ~/.credentials/calendar-api-quickstart.json
not readable or not exists
i assume not pipe in auth-code , samplecode unable find ~/.credentials/calendar-api-quickstart.json
, results in exception since gets no auth-code
Comments
Post a Comment