gpt4 book ai didi

php - 使用 PEAR HTTP_Request2 的 Google Analytics API 中的 "Target feed is read-only"

转载 作者:行者123 更新时间:2023-12-01 17:31:11 37 4
gpt4 key购买 nike

我正在尝试使用 PHP 获取某些 Google Analytics 帐户的所有配置文件。我正在使用 PEAR 中的 HTTP_Request2 类(带有 cURL 适配器,但我也尝试过使用 Socket),当我尝试从 https://www.google.com/analytics/feeds/accounts/default 获取数据时,我不断收到“目标提要是只读的”错误

我正在使用 ClientLogin auth 方法,据我所知,每个 API 请求都会发送正确的授权 header (我使用观察者类来测试正在发送的 header )。

这是我使用的代码(精简版,测试版本):

require 'HTTP/Request2.php';


class GA {

protected $email;
protected $passwd;
protected $auth_code;

public function __construct($email = '', $passwd = '') {
$this->email = $email;
$this->passwd = $passwd;
}

public function authorize($email = '', $password = '', $force = false) {

if (!$force and !empty($this->auth_code) and $email == $this->email and $password == $this->passwd) {
return true;
}

unset($this->auth_code);

!empty($email) or $email = $this->email;
!empty($password) or $password = $this->passwd;

if (empty($email) or empty($password)) {
return false;
}

try {

$response = $this->post(
'https://www.google.com/accounts/ClientLogin',
array(
'accountType' => 'GOOGLE',
'Email' => $this->email = $email,
'Passwd' => $this->passwd = $password,
'service' => 'analytics'
)
);

if ($response->getStatus() == 200 and preg_match('/(?:^|[\n\r])Auth=(.*?)(?:[\n\r]|$)/', $response->getBody(), $match)) {

$this->auth_code = $match[1];
echo $this->auth_code;
return true;
}

} catch (HTTP_Request2_Exception $e) {
return false;
}
}

public function call($url, array $params = array(), array $headers = array()) {

if (!$this->auth_code && !$this->authorize($this->email, $this->passwd, true)) {
return false;
}

$headers['Authorization'] = 'GoogleLogin auth=' . $this->auth_code;

return $this->post($url, $params, $headers);
}

protected function post($url, array $params = array(), array $headers = array()) {

$headers['GData-Version'] = '2';

$request = new HTTP_Request2($url);
$request->setAdapter('curl');
$request->setConfig('ssl_verify_peer', false);
$request->setHeader($headers);
$request->setMethod(HTTP_Request2::METHOD_POST);
$request->addPostParameter($params);

return $request->send();
}
}

$ga = new GA('*********@gmail.com', '*********');

var_dump($ga->call('https://www.google.com/analytics/feeds/accounts/default'));

提前致谢!

最佳答案

回答我自己的问题:https://www.google.com/analytics/feeds/accounts/default 必须通过GET方法访问。我的代码始终使用 POST

关于php - 使用 PEAR HTTP_Request2 的 Google Analytics API 中的 "Target feed is read-only",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2116838/

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