0

我有2個Gmail帳戶(A & B),每個帳戶都有自己獨立的Google 分析帳戶。谷歌分析API顯示錯誤用戶的數據

我創建了以下 http://www.daimto.com/google-oauth2-php/

  • 我登錄與賬戶A和我得到帳戶下的所有配置文件的web應用程序 (這是正確的)

  • 我登錄從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> 

我在做什麼錯了!

有什麼想法嗎?

+0

你能否提供你的代碼和你正在採取的行動? – dikesh

+0

我編輯了我的問題,謝謝:) – Zorox

+0

我的猜測與會議的東西。嘗試session_destroy();在註銷。或者可能未設置($ _ SESSION ['token']);這是我前段時間向人們展示的一個快速教程,我從來沒有去過真正的測試。讓我知道它是否有效。 – DaImTo

回答

0

有意義再次檢查,GA帳戶可以訪問您的「A」Google帳戶,並且GA帳戶可以訪問「B」Google帳戶。

如果被其他瀏覽器下的「B」賬戶在另一臺計算機登錄,看到GA佔「A」,然後是「A」和「B」彼此相關: 看起來上面顯示腳本GA帳戶,可用於當前的GA帳戶。

理論上,這可能因爲happend下列之一:

  • 「B」 有權訪問 「A」 GA賬戶;
  • 可能是Google顯示所有可用的GA帳戶(包括「A」和「B」) - 無法確定;
  • 可能你在想,記錄的是「B」,但是你記錄在「A」谷歌賬戶下(如何檢查:用PHP輸出谷歌賬戶的電子郵件,如果這還沒有完成)。

這些只是想法,希望其中的一些會幫助你。無論如何,我認爲腳本是正確的,問題在於谷歌帳戶和GA訪問。