gpt4 book ai didi

php - 谷歌分析 V3 API PHP

转载 作者:可可西里 更新时间:2023-11-01 01:02:26 25 4
gpt4 key购买 nike

我正在尝试从我的谷歌分析帐户中获取数据。我已经完成了这些步骤:

  1. 为 Analytic 启用 API 访问权限
  2. 创建 OAuth2 和服务器帐户以访问数据
  3. 添加了电子邮件地址(在代码中指定)以访问我的分析。

我在这里使用 PHP 客户端:https://github.com/google/google-api-php-client (阿尔法)

我收到的错误是:出现错误:-(获取)未知参数:“开始日期”

任何帮助都将不胜感激,就像我在这里撞墙一样。

我在下面包含了我的代码。

`

set_include_path("../src/" . PATH_SEPARATOR . get_include_path());
require_once 'Google/Client.php';
require_once 'Google/Service/Analytics.php';

$client_id = 'xxx.apps.googleusercontent.com';
$service_account_name = 'xxx@developer.gserviceaccount.com';
$keyfile = 'xxx-privatekey.p12';
$redirect_url = 'http://xxx/tags/v1.0.0-alpha/examples/analytics.php';
$client_secret = 'xxx';

// Initialise the Google Client object
$client = new Google_Client();
$client->setApplicationName('Your product name');
$client->setRedirectUri($redirect_url);
$client->setClientSecret($client_secret);

$client->setAssertionCredentials(
new Google_Auth_AssertionCredentials(
$service_account_name,
array('https://www.googleapis.com/auth/analytics'),
file_get_contents($keyfile)
)
);

// Get this from the Google Console, API Access page
$client->setClientId($client_id);
$client->setAccessType('offline_access');
$analytics = new Google_Service_Analytics($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:xxx';
$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();
}

`

最佳答案

不确定您是否自己找到了这个问题的答案,但我遇到了同样的问题并通过编辑第 93 行的 apiclient/src/Google/Service/Analytics.php 文件解决了这个问题& 98

          "start_date" => array(
"location" => "query",
"type" => "string",
'required' => true,
),
"end_date" => array(
"location" => "query",
"type" => "string",
'required' => true,
),

需要成为:

        "start-date" => array(
"location" => "query",
"type" => "string",
'required' => true,
),
"end-date" => array(
"location" => "query",
"type" => "string",
'required' => true,
),

关于php - 谷歌分析 V3 API PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20348708/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com