php - Google API Redirect URI not working properly -
i'm new google api , having problem getting analytics api work. far i've set developer console, created project, , generated relevant credentials. registered email id myname@mycompany.com, , redirect uri set http://www.mycompany.com/oauth2callback default. javascript origins set http://www.mycompany.com.
when run project localhost, i'm able initiate oauth procedure. when nail "allow" button, i'm sent http://www.mycompany.comand nil happens. doing wrong? need running script mycompany.com doamin work? lastly, how can run localhost?
<?php include_once "google_api/autoload.php"; session_start(); $client = new google_client(); $client->setapplicationname("hello analytics api example"); $client->setclientid('xxxxxxxxxxxxxxxxx'); $client->setclientsecret('xxxxxxxxxxxxxxxxxxxx'); //$client->setredirecturi('xxxxxxxxxxxxxxxxxxx'); $client->setredirecturi('xxxxxxxxxxxxxxxxxx'); $client->setdeveloperkey('xxxxxxxxxxxxxxxxxxxx'); $client->setscopes(array('https://www.googleapis.com/auth/analytics.readonly')); // magic. returns objects analytics service instead of associative arrays. //print_r($client); //$client->setuseobjects(true); if (isset($_get['code'])) { $client->authenticate(); $_session['token'] = $client->getaccesstoken(); $redirect = 'http://' . $_server['http_host'] . $_server['php_self']; header('location: ' . filter_var($redirect, filter_sanitize_url)); } if (isset($_session['token'])) { $client->setaccesstoken($_session['token']); } if (!$client->getaccesstoken()) { $authurl = $client->createauthurl(); print "<a class='login' href='$authurl'>connect me!</a>"; } else { // create analytics service object. see next step below. $analytics = new apianalyticsservice($client); runmaindemo($analytics); } function runmaindemo(&$analytics) { seek { // step 2. user's first view (profile) id. $profileid = getfirstprofileid($analytics); if (isset($profileid)) { // step 3. query core reporting api. $results = getresults($analytics, $profileid); // step 4. output results. printresults($results); } } grab (apiserviceexception $e) { // error api. print 'there api error : ' . $e->getcode() . ' : ' . $e->getmessage(); } grab (exception $e) { print 'there wan general error : ' . $e->getmessage(); } } function getfirstprofileid(&$analytics) { $accounts = $analytics->management_accounts->listmanagementaccounts(); if (count($accounts->getitems()) > 0) { $items = $accounts->getitems(); $firstaccountid = $items[0]->getid(); $webproperties = $analytics->management_webproperties ->listmanagementwebproperties($firstaccountid); if (count($webproperties->getitems()) > 0) { $items = $webproperties->getitems(); $firstwebpropertyid = $items[0]->getid(); $profiles = $analytics->management_profiles ->listmanagementprofiles($firstaccountid, $firstwebpropertyid); if (count($profiles->getitems()) > 0) { $items = $profiles->getitems(); homecoming $items[0]->getid(); } else { throw new exception('no views (profiles) found user.'); } } else { throw new exception('no webproperties found user.'); } } else { throw new exception('no accounts found user.'); } } function getresults(&$analytics, $profileid) { homecoming $analytics->data_ga->get( 'ga:' . $profileid, '2012-03-03', '2012-03-03', 'ga:sessions'); } function printresults(&$results) { if (count($results->getrows()) > 0) { $profilename = $results->getprofileinfo()->getprofilename(); $rows = $results->getrows(); $sessions = $rows[0][0]; print "<p>first view (profile) found: $profilename</p>"; print "<p>total sessions: $sessions</p>"; } else { print '<p>no results found.</p>'; } }
if want able request there info automated need store refreshtoken. if want them able access there info when on site should started.
the latest google php client lib can found here. github
<?php require_once 'google/client.php'; require_once 'google/service/analytics.php'; session_start(); $client = new google_client(); $client->setapplicationname("client_library_examples"); $client->setdeveloperkey("{devkey}"); $client->setclientid('{clientid}.apps.googleusercontent.com'); $client->setclientsecret('{clientsecret}'); $client->setredirecturi('http://www.daimto.com/tutorials/php/oauth2.php'); $client->setscopes(array('https://www.googleapis.com/auth/analytics.readonly')); //for loging out. if ($_get['logout'] == "1") { 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 (!$client->getaccesstoken() && !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'])) { print "<a class='logout' href='".$_server['php_self']."?logout=1'>logout</a><br>"; $client->setaccesstoken($_session['token']); $service = new google_service_analytics($client); // request user accounts $accounts = $service->management_accountsummaries->listmanagementaccountsummaries(); foreach ($accounts->getitems() $item) { echo "account: ",$item['name'], " " , $item['id'], "<br /> \n"; foreach($item->getwebproperties() $wp) { echo ' webproperty: ' ,$wp['name'], " " , $wp['id'], "<br /> \n"; $views = $wp->getprofiles(); if (!is_null($views)) { foreach($wp->getprofiles() $view) { // echo ' view: ' ,$view['name'], " " , $view['id'], "<br /> \n"; } } } } // closes business relationship summaries } print "<br><br><br>"; print "access google: " . $_session['token']; ?> code ripped tutorial google analytics oauth2 php.
update: after looking @ code think part of problem may aren't linking redirecturi page. cant directory must give php page should redirect to.
php google-analytics-api
No comments:
Post a Comment