如何使用Google Analytics PHP API列出管理配置文件?

如何使用Google Analytics PHP API列出管理配置文件?,php,google-analytics,google-api,google-analytics-api,google-api-php-client,Php,Google Analytics,Google Api,Google Analytics Api,Google Api Php Client,我正在尝试使用最新的PHP客户端API()检索谷歌分析管理配置文件 我有以下代码片段: // we've got the token, so set it $google_client->setAccessToken($_SESSION['access_code']); if ($google_client->getAccessToken()) { $profiles = $google_analytics_service->management_profiles-&

我正在尝试使用最新的PHP客户端API()检索谷歌分析管理配置文件

我有以下代码片段:

// we've got the token, so set it
$google_client->setAccessToken($_SESSION['access_code']);

if ($google_client->getAccessToken()) {
    $profiles = $google_analytics_service->management_profiles->listManagementProfiles("~all", "~all");
    print "<h1>Profiles</h1><pre>" . print_r($profiles, true) . "</pre>";
}

/* $url = 'https://www.googleapis.com/analytics/v3/management/accounts/~all/webproperties/~all/profiles'; */
/* // json decode as array */
/* $analytics_auth = json_decode($_SESSION['access_code'], true); */

/* $ch = curl_init($url . '?access_token=' . $analytics_auth['access_token']); */
/* curl_exec($ch); */
/* curl_close($ch); */
//我们有令牌了,所以设置它
$google_client->setAccessToken($_会话['access_code']);
如果($google\u client->getAccessToken()){
$profiles=$google_analytics_service->management_profiles->listManagementProfiles(“~all”和“~all”);
打印“配置文件”。打印($Profiles,true)。“”;
}
/*$url='1https://www.googleapis.com/analytics/v3/management/accounts/~all/webproperties/~all/profiles'*/
/*//json解码为数组*/
/*$analytics_auth=json_decode($_SESSION['access_code'],true)*/
/*$ch=curl\u init($url.'?访问令牌='。$analytics\u auth['access\u token'])*/
/*curl_exec($ch)*/
/*卷曲关闭($ch)*/
我从上面得到的错误消息是:

调用GET时出错:(403)未配置访问权限


注意:但是我决定用cURL运行相同的函数,它返回一个带有概要文件的JSON数组(注释代码)。这是虫子还是我?我注意到我的访问令牌以“ya29”开头

我认为你错过了一步:



由于会话启动和头出现问题,它有点不正常。我添加了一些注释,以帮助您了解它的作用。这是一个简单的脚本,但是您可以在这里测试它。403问题是因为没有为服务器应用程序密钥设置正确的IP地址。这可以在谷歌开发者控制台中设置。然而,奇怪的是,使用cURL忽略了这一点

公共API访问 使用此密钥不需要任何用户操作或同意,不授予访问任何帐户信息的权限,也不用于授权

服务器应用程序API密钥:AI[snip] IP:91。[snip]109。[snip]确保此处设置了正确的IP地址


我有类似于你上面的代码,但只是在问题中发布了我的“片段”。我复制并粘贴了您的代码,并得到以下错误:致命错误:未捕获异常“Google_Service_exception”,消息为“error calling GET:(403)Access Not Configured”。请使用Google Developers Console激活您项目的API。我可以确认API在我的Google Developers Console中处于活动状态(我使用cURL的原始代码工作正常)。您确定在Google Cloud Developer Console中为应用激活了Google Analytics API吗?API&Auth->API->Google Analytics打开。是的,我可以确认这是活动的。我也可以证明这一点,因为我的cURL请求(在原始示例中注释掉)返回JSON。当您使用我的代码时,您将clientid内容更改为您的,对吗?在您的代码中,您正在初始化$google\u analytics\u service->对吗?是的,当然。我将我的cURL请求添加到您的代码中,并将其打印出来:{“种类”:“分析#配置文件”,“用户名”:“[snip]”,“totalResults”:1,“startIndex”:1,“itemsPerPage”:1000,“items”:[{“id”:“[snip]”,“种类”:“分析#配置文件”,“自链接”:“,。。。
   <?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("{developerkey}");  
    $client->setClientId('{clientID}.apps.googleusercontent.com');
    $client->setClientSecret('{Client secret}');
    $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>"; // fix syntax
 print "Access from google: " . $_SESSION['token']; 


?>