本文介绍了Google Analytics API自动登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的公司正在开发此面板,我将在Google Analytics中显示用户的信息,但我不希望用户在每次访问面板时授权或登录其帐户。 / p>

我想要做的是:第一次使用我的面板时,他会连接他的Google帐户,我会保存一些信息,并在下一次他连接到我的面板我会使用这个已保存的信息登录他的帐户,这样我就可以列出Google Analytics信息而无需询问他的权限,或者列出该信息,即使他没有连接也是Google帐户。



基本上我会自动登录他的帐户,并允许应用程序显示信息。



我已经有一些代码连接到API他连接的是Google帐户,但是当他没有登录屏幕时,他必须提供他的电子邮件密码。



到目前为止,我所拥有的是:

   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);
$ b $ 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']) ;

$ b $ if($ client-> getAccessToken())
{
$ props = $ service-> management_webproperties-> listManagementWebproperties(12008145 ); //〜全部
打印< h1>网络媒体资源< / h1>< pre> 。 print_r($ props,true)。 < /预> 中;

$ accounts = $ service-> management_accounts-> listManagementAccounts();
//打印< h1>帐户< / h1>< pre> 。 print_r($ accounts,true)。 < /预> 中;

$ segments = $ service-> management_segments-> listManagementSegments();
//打印< h1>细分< / h1>< pre> 。 print_r($ segments,true)。 < /预> 中;

$ goals = $ service-> management_goals-> listManagementGoals(〜all,〜all,〜all);
//打印< h1>目标< / h1>< pre> 。 print_r($目标,true)。 < /预> 中;

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

有没有办法做到这一点?

解决方案

在google的Api中,当用户验证第一个时间,你收到一个代码(你已经得到我想)。使用此代码获取刷新标记(生存期是(总是),直到用户撤销权限为止)。将此刷新标记保存在数据库中以备将来使用。刷新令牌用于获取访问令牌(生命期很短,在expires参数中返回)。访问令牌可让您访问您的用户数据一段时间。每当您需要访问用户数据时,您都可以继续使用刷新令牌来获取访问令牌。



无论何时您想要访问用户数据,请使用刷新令牌获取访问令牌,然后使用该访问令牌来获取用户的数据。



在你的情况下,你使用的是google api php客户端,你可以在Client.php中使用方法如下:

getAccessToken() ---第一次获取刷新标记。当你调用这个方法时,你会返回一个表单中的json:让这个json名称为$ accessToken



$ p $ code $ $ accessToken = { access_token:TOKEN,refresh_token:TOKEN,token_type:承载者,
expires_in:3600,id_token:token,created:1320790426} $ b

解析json以获取refresh_token($ refreshToken = $ accessToken.refresh_token)并保存以备后用。



setAccessToken($ accessToken) ---调用此设置来设置OAuth访问令牌。





为了进一步清晰起见,请查看

refreshToken($ refreshToken) Client.php并阅读:




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.

解决方案

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

这篇关于Google Analytics API自动登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 05:37