0

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

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

  • 我使用帐户 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>

我究竟做错了什么!

任何想法?

4

1 回答 1

0

再次检查一下,哪些 GA 帐户可以访问您的“A”google 帐户,以及哪些 GA 帐户可以访问“B”google 帐户,这很有意义。

如果您从另一台计算机的“B”帐户下的另一个浏览器登录并看到“A”的 GA 帐户,则“A”和“B”是相互关联的:看起来上面的脚本显示GA 帐户,当前可用GA 帐户。

从理论上讲,这可能是由于以下原因之一:

  • “B”有权访问“A”GA账户;
  • 可能是 Google 显示了所有可用的 GA 帐户(“A”和“B”)——对此不确定;
  • 可能你在想,由“B”记录,但你登录在“A”谷歌帐户下(如何检查:使用 PHP 在页面上输出谷歌帐户电子邮件,如果尚未完成)。

这些只是想法,希望其中一些对您有所帮助。无论如何,我认为脚本是正确的,问题出在谷歌帐户和 GA 访问权限上。

于 2016-01-05T13:11:57.347 回答