Google Analytics API Automated Login

亡梦爱人 提交于 2019-12-12 02:43:53

问题


I have this panel that i'm developing at my company where i will show the user's information from Google Analytics but i don't want the user to authorize or log in with his account every time he comes to the panel.

What i would like to do is: on the first time using my panel he would connect his Google account and i would save some info and on the next time he connects at my panel i would use this saved info to log on his account so i can list the Analytics info without ask for his permission or list that info even if he's not connected on is Google account right now.

Basically i would log in his account automatically and permit the 'app' to show the information.

I already have some code that connects on the API if he is connected on is Google account, but when he's not i get the login screen where he has to provide his email e password.

What i have so far is this:

<?php
require_once 'Google/Client.php';
require_once 'Google/Service/Analytics.php';

session_start();

$client = new Google_Client();
$client->setApplicationName("Google Analytics PHP Starter Application");

$client->setClientId('KEY');
$client->setClientSecret('SECRET');
$client->setRedirectUri('RETURN URI');
$client->setScopes('https://www.googleapis.com/auth/analytics.readonly');
$client->setAccessType('offline');
$service = new Google_Service_Analytics($client);

if(isset($_GET['logout']))
{
  unset($_SESSION['token']);
}

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));
}

if(isset($_SESSION['token']))
{
  $client->setAccessToken($_SESSION['token']);
}

if($client->getAccessToken())
{
  $props = $service->management_webproperties->listManagementWebproperties("12008145");//~all
  print "<h1>Web Properties</h1><pre>" . print_r($props, true) . "</pre>";

  $accounts = $service->management_accounts->listManagementAccounts();
  //print "<h1>Accounts</h1><pre>" . print_r($accounts, true) . "</pre>";

  $segments = $service->management_segments->listManagementSegments();
  //print "<h1>Segments</h1><pre>" . print_r($segments, true) . "</pre>";

  $goals = $service->management_goals->listManagementGoals("~all", "~all", "~all");
  //print "<h1>Goals</h1><pre>" . print_r($goals, true) . "</pre>";

  $_SESSION['token'] = $client->getAccessToken();
}
else
{
  $authUrl = $client->createAuthUrl();
  header("Location: " . $authUrl);
}
?>

Is there any way to do that ? I have looked for it around everywhere and couldn't find something near it.


回答1:


In google Api's, when user authenticate the first time, you receive a CODE (which you are already getting i suppose). Use this code to get refresh token (lifetime is (always), until and unless, user revokes the permissions). Save this refresh token in Database for further use. Refresh token is used to get access token(lifetime is a short time, returned in the expires in argument). Access token is to give you access to your user's data for some time. You can keep using refresh token to get access token whenever you need to access your user's data.

Whenever you want to access user's data, use refresh token to get access token and then use that access token to get user's data.

In your case, you are using google api php client, you can use Methods in Client.php like:

getAccessToken()---to get refresh token the first time. When you call this method, you get back a json in a form: let this json name be $accessToken

$accessToken = {"access_token":"TOKEN", "refresh_token":"TOKEN", "token_type":"Bearer",
"expires_in":3600,"id_token":"TOKEN", "created":1320790426}

parse json to take refresh_token($refreshToken = $accessToken.refresh_token) and save it for later use.

setAccessToken($accessToken)---call this to set the OAuth access token.

refreshToken($refreshToken)---Fetches a fresh OAuth access token with the given refresh token.

For further clearity, look at Client.php and also read:

https://developers.google.com/accounts/docs/OAuth2WebServer



来源:https://stackoverflow.com/questions/21737673/google-analytics-api-automated-login

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!