php - Google API and OAuth 2.0 -
i'm trying use google calendar api php library , i'm facing issues on authentification of user google api.
i have question. i've seen come had set api key / developer key google_client object method setdeveloperkey(), i've seen people don't. explain me difference make ?
the thing i'd connect user have google account application can add, list, remove, etc, events calendar. i'm doing moment authentification :
$client = new google_client(); $client->setapplicationname("test gcal"); $client->setclientid($clientid); $client->setclientsecret($clientsecret); $client->setredirecturi($callback_url); $client->setaccesstype("offline"); $client->setapprovalprompt("force"); $client->setscopes("https://www.googleapis.com/auth/calendar"); $service = new google_service_calendar($client);
am doing right ?
does have working commented code can analyse ? can't find 1 that's working on internet.. or maybe tutorial explain google api , oauth stuff. i'm confused tokens , nobody seems use refresh tokens, , me that's essential.. maybe i'm wrong ?
thanks answers
i don't think need use setdeveloperkey
suspect used public apis enable use them haven't tested or thought before. have bit more.
this code use connecting google calendar oauth2. ripped directly accessing google calendar php – oauth2 tutorial
<?php require_once 'google/client.php'; require_once 'google/service/calendar.php'; require_once 'calendarhelper.php'; session_start(); $client = new google_client(); $client->setapplicationname("client_library_examples"); $client->setdeveloperkey("aizasybbh88diqpjcl5nig-n1mmuq12j7hthdbe"); $client->setclientid('2046123799103-i6cjd1hkjntu5bkdkjj5cdnpcu4iju8p.apps.googleusercontent.com'); $client->setclientsecret('6s4yox3upyjhtwnetovfk40e'); $client->setredirecturi('http://localhost/google-api-php-client-samples/calendar/oauth2pure.php'); $client->setaccesstype('offline'); // gets our refreshtoken $client->setscopes(array('https://www.googleapis.com/auth/calendar.readonly')); //for loging out. if (isset($_get['logout'])) { unset($_session['token']); } // step 2: user accepted access need exchange it. if (isset($_get['code'])) { $client->authenticate($_get['code']); $_session['token'] = $client->getaccesstoken(); $redirect = 'http://' . $_server['http_host'] . $_server['php_self']; header('location: ' . filter_var($redirect, filter_sanitize_url)); } // step 1: user has not authenticated give them link login if (!isset($_session['token'])) { $authurl = $client->createauthurl(); print "<a class='login' href='$authurl'>connect me!</a>"; } // step 3: have access can create our service if (isset($_session['token'])) { $client->setaccesstoken($_session['token']); print "<a class='logout' href='".$_server['php_self']."?logout=1'>logout</a><br>"; $service = new google_service_calendar($client); $calendarlist = $service->calendarlist->listcalendarlist();; print_r($calendarlist); while(true) { foreach ($calendarlist->getitems() $calendarlistentry) { echo $calendarlistentry->getsummary()."<br>\n"; } $pagetoken = $calendarlist->getnextpagetoken(); if ($pagetoken) { $optparams = array('pagetoken' => $pagetoken); $calendarlist = $service->calendarlist->listcalendarlist($optparams); } else { break; } } } ?>
Comments
Post a Comment