如何在不登录的情况下使用 google api 获取分析数据?

Posted

技术标签:

【中文标题】如何在不登录的情况下使用 google api 获取分析数据?【英文标题】:How to get analytics data using google api without login? 【发布时间】:2014-05-15 04:12:16 【问题描述】:

我正在使用 phpmysql。我必须使用 google sdk api 获取不同站点的分析数据,最重要的是我不需要登录 api。没有登录我需要去获取分析数据。 请查看下面的代码,我可以在其中获取“Google_AnalyticsService”对象并在获取数据时遇到错误。它说需要登录...

require 'apigp/Google_Client.php';
require 'apigp/contrib/Google_PlusService.php';
require 'apigp/contrib/Google_AnalyticsService.php';

date_default_timezone_set('UTC');
$date_beforeonemoth = date("Y-m-d", mktime(0,0,0,date("m"),date("d")-30,date("Y")));
$date_today = date("Y-m-d", mktime(0,0,0,date("m"),date("d"),date("Y")));

$google = new Google_Client();
$google->setDeveloperKey('Google AppKey here');
$google->setClientSecret('Google ClientSecretKey here');
$google->setClientId('Google ClientID here');
$analytics = new Google_AnalyticsService($google);

$profileURL="";
$pageName="";
$brandId="";
$sql="select * from profiles where APIType='Google Plus'";
$result=mysql_query($sql);
while($rs = mysql_fetch_assoc($result))

    $profileURL='https://plus.google.com/105932700878744xxxxxx';
    $brandId=4;
    $pageName=basename($profileURL);

    $accounts = $analytics->management_accounts->listManagementAccounts();
    if(isset($accounts['totalResults']))
        if($accounts['totalResults']>0)
            $firstAccountId = $accounts['items'][0]['id'];
 $webproperties = $analytics->management_webproperties->listManagementWebproperties($firstAccountId);
                if(count($webproperties['items'])>0)
                     $firstWebpropertyId = $webproperties['items'][0]['id'];
$profiles = $analytics->management_profiles->listManagementProfiles($firstAccountId, $firstWebpropertyId);

                     if (count($profiles['items']) > 0) 
                       $profileId = $profiles['items'][0]['id'];


        $profileId = $pageName;
        $results = $analytics->data_ga->get(
        'ga:' . $profileId,
        $date_beforeonemoth,
        $date_today,
       'ga:visits,ga:pageviews,ga:pageviewsPerVisit,ga:avgTimeOnPage,ga:visitBounceRate,ga:percentNewVisits,ga:uniquePageviews',
        array(
            'dimensions' => 'ga:source,ga:keyword,ga:browser,ga:date,ga:month,ga:day,ga:year',
            'sort' => '-ga:visits,ga:keyword',
           // 'filters' => 'ga:medium==organic',
            'max-results' => '25'));

                       $visits = "";
                       $unique_visitors = "";
                       $pageviews = "";
                       $pages_visit = "";
                       $avg_visit_duration = "";
                       $bounce_rate = "";
                       $new_visits = "";

                       echo "<br>".$visits = $results['totalsForAllResults']['ga:visits'];
                       echo "<br>".$unique_visitors = $results['totalsForAllResults']['ga:uniquePageviews'];
                       echo "<br>".$pageviews = $results['totalsForAllResults']['ga:pageviews'];
                       $pages_visit = $results['totalsForAllResults']['ga:pageviewsPerVisit'];
                       $avg_visit_duration = $results['totalsForAllResults']['ga:avgTimeOnPage'];
                       $bounce_rate = $results['totalsForAllResults']['ga:visitBounceRate'];
                       $new_visits = $results['totalsForAllResults']['ga:percentNewVisits'];

                       $profileName = $results['profileInfo']['profileName'];

                    elseprint"No profiles found for this user.";

                    elseprint"No webproperties found for this user.";  //close webproperties result if condition

                elseprint"No accounts found for this user."; //close total result if condition   


错误: 致命错误:E:\xampp\htdocs\DATAPOINTS\apigp\io\Google_REST.php:66 中的消息“调用 GET https://www.googleapis.com/analytics/v3/management/accounts?key=AIzaSyBeh0Aevex7q3iRIY5bV3N9gx0WAkNBMi4 时出错:(401) 需要登录”的未捕获异常“Google_ServiceException”:66 堆栈跟踪:#0 E: \xampp\htdocs\DATAPOINTS\apigp\io\Google_REST.php(36): Google_REST::decodeHttpResponse(Object(Google_HttpRequest)) #1 E:\xampp\htdocs\DATAPOINTS\apigp\service\Google_ServiceResource.php(186): Google_REST::execute(Object(Google_HttpRequest)) #2 E:\xampp\htdocs\DATAPOINTS\apigp\contrib\Google_AnalyticsService.php(139): Google_ServiceResource->__call('list', Array) #3 E:\xampp\ htdocs\DATAPOINTS\anatest.php(33): Google_ManagementAccountsServiceResource->listManagementAccounts() #4 main 在第 66 行的 E:\xampp\htdocs\DATAPOINTS\apigp\io\Google_REST.php 中抛出

如何在不登录api的情况下获取分析数据?

【问题讨论】:

【参考方案1】:

根据 Google 的规定,您必须在每个 API 请求中发送授权凭据。

请看这里:https://developers.google.com/analytics/devguides/reporting/core/v3/#user_reports

【讨论】:

我可以通过以下代码获取 google plus 凭据:$plus = new Google_PlusService($google); $datas = $plus->people->get('10593270087874xxxxxxx');我想做同样的事情来获取分析数据。该怎么做?

以上是关于如何在不登录的情况下使用 google api 获取分析数据?的主要内容,如果未能解决你的问题,请参考以下文章

如何在不使用API 的情况下获得Google云端硬盘的可用空间?

如何在不使用API 的情况下访问Google课堂?

如何在不使用dotnet中的安全令牌(使用soap api)的情况下登录salesforce?

如何在不使用google-api-nodejs-client的情况下处理Google API access_token已过期?

如何在不注册身份的情况下从外部提供商获取用户登录信息

在不打开新窗口/标签的情况下使用 Google 身份验证登录