php - Where to optain Google Analytics API access_token? -



php - Where to optain Google Analytics API access_token? -

according google analytics api reference oauth access token can used access stats of page. using oauth pretty complicated or @ to the lowest degree seems me in php. according docs should able utilize access_code parameter.

i tried several ways access_token google developer console, neither working returning next error:

{"error":{"errors":[{"domain":"global","reason":"required","message":"login required","locationtype":"header","location":"authorization"}],"code":401,"message":"login required"}}

thus i'm wondering how none expireing api access code google analytics page properties? if possible @ all?

you need register application on google developer console first. matter of using oauth2 access token. access token first asking user permission access data. 1 time have been given permission receive refresh token can utilize access token.

the next illustration uses google php client lib found on github. code copied google oauth2 php tutorial tutorial.

<?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 '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;webproperty: ' ,$wp['name'], " " , $wp['id'], "<br /> \n"; $views = $wp->getprofiles(); if (!is_null($views)) { foreach($wp->getprofiles() $view) { // echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;view: ' ,$view['name'], " " , $view['id'], "<br /> \n"; } } } } // closes business relationship summaries } print "<br><br><br>"; print "access google: " . $_session['token']; ?>

php api oauth google-analytics google-analytics-api

Comments

Popular posts from this blog

java Multi query from Mysql using netbeans -

c# - DotNetZip fails with "stream does not support seek operations" -

c++ - StartServiceCtrlDispatcher don't can access 1063 error -