php - google analytics (401) Login Required error


i am using google analytics php api to retrieve my data. on index.php i have authorization process done which works fine and i get successful login on this page i have link to another php page where i want to display all my properties so how i can post my login credentials to properties.php and create an analytics object to retrieve all the webproperties

code index.php look like this

require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_AnalyticsService.php';

$scriptUri = "http://".$_SERVER["HTTP_HOST"].$_SERVER['PHP_SELF'];

$client = new Google_Client();
$client->setAccessType('online'); // default: offline
$client->setDeveloperKey('XXXXXX'); // API key

$service = new Google_AnalyticsService($client);
if (isset($_GET['logout'])) { // logout: destroy token
    die('Logged out.');

if (isset($_GET['code'])) { // we received the positive auth callback, get the token and store it in session
    $_SESSION['token'] = $client->getAccessToken();

if (isset($_SESSION['token'])) { // extract token from session and configure client
    $token = $_SESSION['token'];

if (!$client->getAccessToken()) { // auth call to google
    $authUrl = $client->createAuthUrl();
    header("Location: ".$authUrl);



People are also looking for solutions to the problem: php - Parsing mysql with html camp to xml


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.