在哪里获取Google Analytics API access_token


Where to obtain Google Analytics API access_token?

根据Google Analytics API,参考OAuth和访问令牌可用于访问页面的统计数据。使用OAuth相当复杂,或者至少在我看来,它在php中是这样的。然而,根据文档,我也应该能够使用access_code作为get参数。

我尝试了几种方法从谷歌开发者控制台获取access_token,但都不起作用,总是返回以下错误:

{"error":{"errors":[{"domain":"global","reason":"required","message":"Login Required","locationType":"header","location":"Authorization"}],"code":401,"message":"Login Required"}}

因此,我想知道如何为我的谷歌分析页面属性获得一个无过期的api访问代码?如果可能的话?

您需要首先在Google Developer控制台上注册您的应用程序。这是一个使用Oauth2获取访问令牌的问题。您可以通过首先向用户请求访问其数据的权限来获得访问令牌。一旦获得许可,您将收到一个刷新令牌,您可以使用该令牌来获取访问令牌。

以下示例使用GitHub上的GooglePHP客户端库。这段代码是从我的Google Oauth2 PHP教程中复制的。

<?php         
require_once 'Google/Client.php';     
require_once 'Google/Service/Analytics.php';       
session_start();      
$client = new Google_Client();
    $client->setApplicationName("Client_Library_Examples");
    $client->setDeveloperKey("{devkey}");  
    $client->setClientId('{clientid}.apps.googleusercontent.com');
    $client->setClientSecret('{clientsecret}');
    $client->setRedirectUri('http://www.daimto.com/Tutorials/PHP/Oauth2.php');
    $client->setScopes(array('https://www.googleapis.com/auth/analytics.readonly'));
    //For loging out.
    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'])) {
        print "<a class='logout' href='".$_SERVER['PHP_SELF']."?logout=1'>LogOut</a><br>";
        $client->setAccessToken($_SESSION['token']);
        $service = new Google_Service_Analytics($client);    
        // request user accounts
        $accounts = $service->management_accountSummaries->listManagementAccountSummaries();
       foreach ($accounts->getItems() as $item) {
        echo "Account: ",$item['name'], "  " , $item['id'], "<br /> 'n";        
        foreach($item->getWebProperties() as $wp) {
            echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WebProperty: ' ,$wp['name'], "  " , $wp['id'], "<br /> 'n";    
            $views = $wp->getProfiles();
            if (!is_null($views)) {
                foreach($wp->getProfiles() as $view) {
                //  echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;View: ' ,$view['name'], "  " , $view['id'], "<br /> 'n";    
                }
            }
        }
    } // closes account summaries
    }
 print "<br><br><br>";
 print "Access from google: " . $_SESSION['token']; 
?>