Google Analytics API显示错误用户的数据

时间:2016-01-04 00:45:07

标签: oauth google-analytics oauth-2.0 google-analytics-api google-analytics-sdk

我有2个gmail帐户(A& B),每个帐户都有自己独立的谷歌    分析帐户。

我创建了一个Web应用程序    http://www.daimto.com/google-oauth2-php/

  • 我使用帐户A登录,然后获得帐户A下的所有个人资料 (这是正确的)

  • 我使用另一台计算机上的帐号B登录,然后我获得了帐户" A" 帐户!!!!!!!

代码如下:

<?php    
require_once 'google-api-php-client/vendor/autoload.php';
session_start(); 

// ********************************************************  //
// Get these values from https://console.developers.google.com
// Be sure to enable the Analytics API
// ********************************************************    //
$client_id = 'xxxxxxxxxxxxxx';
$client_secret = 'xxxxxxxxxxxxxx';
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];

$client = new Google_Client();
$client->setApplicationName("MY_APP_NAME");
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->setState(md5(uniqid(rand(), true)));
$client->setScopes(array('https://www.googleapis.com/auth/userinfo.email','https://www.googleapis.com/auth/userinfo.profile','https://www.googleapis.com/auth/analytics.readonly','https://www.googleapis.com/auth/analytics.manage.users.readonly'));
$client->setAccessType('offline');   // Gets us our refreshtoken

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

// Step 2: The user accepted your access now you need to 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:  The user has not authenticated we give them a link to login    
if (!$client->getAccessToken() && !isset($_SESSION['token'])) {
    $authUrl = $client->createAuthUrl();
    print "<a class='login' href='$authUrl'>Connect Me!</a>";
}    

// Step 3: We have access we can now create our service
if (isset($_SESSION['token'])) {
    d($_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);   
    $google_oauth =new Google_Service_Oauth2($client);
    $google_account = $google_oauth->userinfo->get();       
    // request user accounts
    $accounts = $service->management_accountSummaries->listManagementAccountSummaries();
    foreach ($accounts->getItems() as $item) {
    echo "<b>Account:</b> ",$item['name'], " : " , $item['id'], "<br /> \n";
    foreach($item->getWebProperties() as $wp) {
        echo '-----<b>WebProperty:</b> ' ,$wp['name'], " : " , $wp['id'], "<br /> \n";    
        $views = $wp->getProfiles();
        if (!is_null($views)) {
            foreach($wp->getProfiles() as $view) {
                echo '----------<b>View:</b> ' ,$view['name'], " : " , $view['id'], "<br /> \n";    
            }
        }
    }
} 
}
function d($o){
    echo("<pre>");
    print_r($o); 
    echo("</pre>");
}
?>



The tutorial for this file can be found at <a href='http://www.daimto.com/google-oauth2-php/'>Google Oauth php</a><br>

我做错了什么!

任何想法?

1 个答案:

答案 0 :(得分:0)

有意义再次检查,GA帐户可以访问您的&#34; A&#34;谷歌帐户,以及GA帐户有权访问&#34; B&#34;谷歌帐户。

如果您在另一台计算机上使用另一台浏览器进行了记录,那么&#34; B&#34;帐户和看到GA帐户&#34; A&#34;,然后&#34; A&#34;和&#34; B&#34;相互关联: 看上面的脚本显示 GA帐户,可用于当前的GA帐户。

理论上,这可能是因为以下之一:

  • &#34; B&#34;可以访问&#34; A&#34; GA帐户;
  • Google可能会显示所有可用的GA帐户(包括&#34; A&#34;以及&#34; B&#34;) - 对此不确定;
  • 可能是你在思考,记录在&#34; B&#34;,但是你被记录在&#34; A&#34;谷歌帐户(如何检查:使用PHP在页面上输出谷歌帐户电子邮件,如果这还没有完成)。

这些只是想法,希望他们中的一些人会帮助你。无论如何,我认为脚本是正确的,问题出在Google帐户和GA访问上。

相关问题