服务应用程序和Google Analytics API V3:错误101(net :: ERR_CONNECTION_RESET) [英] Service Applications and Google Analytics API V3: Error 101 (net::ERR_CONNECTION_RESET)

查看:2124
本文介绍了服务应用程序和Google Analytics API V3:错误101(net :: ERR_CONNECTION_RESET)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用PHP获取我的Google Analytics帐户的一些信息.我已经按照在此答案中的步骤在Google Console API中创建服务帐户.我正在使用适用于PHP的Google API客户端.

这是我到目前为止获得的代码:

<?php
$path_to_src = 'src';

// These files are in /src, upload its contents to your web server
require_once $path_to_src . '/Google_Client.php';               
require_once $path_to_src . '/contrib/Google_AnalyticsService.php';

$path_to_keyfile = '***'; //my private key


// Initialise the Google Client object
$client = new Google_Client();

// Your 'Product name'
$client->setApplicationName('My App Name');

$client->setAssertionCredentials(
    new Google_AssertionCredentials(        
        '**', //gserviceaccount mail
        array('https://www.googleapis.com/auth/analytics.readonly'),
        file_get_contents($path_to_keyfile)
    )
);

// Get this from the Google Console, API Access page
$client->setClientId('***'); // my cliente ID
$client->setAccessType('offline_access');
$analytics = new Google_AnalyticsService($client);

// create service and get data
$service = new Google_AnalyticsService($client);

// We have finished setting up the connection,
// now get some data and output the number of visits this week.

// Your analytics profile id. (Admin -> Profile Settings -> Profile ID)
$analytics_id   = 'ga:****'; // my profile id
$lastWeek       = date('Y-m-d', strtotime('-1 week'));
$today          = date('Y-m-d');

try {
    $results = $analytics->data_ga->get($analytics_id, 
                        $lastWeek, 
                        $today,'ga:visits');
    echo '<b>Number of visits this week:</b> ';
    echo $results['totalsForAllResults']['ga:visits'];
} catch(Exception $e) {
    echo 'There was an error : - ' . $e->getMessage();
}

我已经在PHP中启用了openssl扩展名:

当浏览到php脚本的位置时,我几乎会永远加载并出现以下错误:

我正在使用PHP 5.4.7:

调试Google API客户端代码后,脚本似乎在此行中断:

if (!openssl_sign($data, $signature, $this->privateKey, "sha256"))

此行以下的任何内容均不会被调用.看起来错误发生在此行中.这里有不兼容之类的东西吗?

解决方案

对于初学者,您应该更改一件事:

您两次实例化AnalyticsService.取出不使用的那个:

$service = new Google_AnalyticsService($client);

看看这是否完全可以解决您的问题.

I'm trying to get some info of my Google Analytics account using PHP. I already followed the steps for creating a Service Account in the Google Console API in this answer. I'm using the Google API Client for PHP.

This is the code I've got so far:

<?php
$path_to_src = 'src';

// These files are in /src, upload its contents to your web server
require_once $path_to_src . '/Google_Client.php';               
require_once $path_to_src . '/contrib/Google_AnalyticsService.php';

$path_to_keyfile = '***'; //my private key


// Initialise the Google Client object
$client = new Google_Client();

// Your 'Product name'
$client->setApplicationName('My App Name');

$client->setAssertionCredentials(
    new Google_AssertionCredentials(        
        '**', //gserviceaccount mail
        array('https://www.googleapis.com/auth/analytics.readonly'),
        file_get_contents($path_to_keyfile)
    )
);

// Get this from the Google Console, API Access page
$client->setClientId('***'); // my cliente ID
$client->setAccessType('offline_access');
$analytics = new Google_AnalyticsService($client);

// create service and get data
$service = new Google_AnalyticsService($client);

// We have finished setting up the connection,
// now get some data and output the number of visits this week.

// Your analytics profile id. (Admin -> Profile Settings -> Profile ID)
$analytics_id   = 'ga:****'; // my profile id
$lastWeek       = date('Y-m-d', strtotime('-1 week'));
$today          = date('Y-m-d');

try {
    $results = $analytics->data_ga->get($analytics_id, 
                        $lastWeek, 
                        $today,'ga:visits');
    echo '<b>Number of visits this week:</b> ';
    echo $results['totalsForAllResults']['ga:visits'];
} catch(Exception $e) {
    echo 'There was an error : - ' . $e->getMessage();
}

I've enabled the openssl extension in PHP:

When browsing to the location of the php script, I just get a almost forever loading and the following error:

I'm using PHP 5.4.7:

After debuging the Google API Client code, it looks like the script is breaking at this line:

if (!openssl_sign($data, $signature, $this->privateKey, "sha256"))

Anything below this line does not get called. Looks like the error happens in this line. Is there a incompatibility here, or something?

解决方案

One thing for starters you should change:

You instantiate the AnalyticsService twice. Take out the one you're not using:

$service = new Google_AnalyticsService($client);

See if that helps your problem at all.

这篇关于服务应用程序和Google Analytics API V3:错误101(net :: ERR_CONNECTION_RESET)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆