doumi7861 2010-01-22 11:23
浏览 44
已采纳

使用PEAR HTTP_Request2在Google AnalyticsAPI中“目标Feed是只读的”

I'm trying to fetch all profiles for some Google Analytics account in PHP. I'm using HTTP_Request2 class from PEAR (with cURL adapter, but I've also tried with Socket) and I keep getting "Target feed is read-only" error when I try to fetch data from https://www.google.com/analytics/feeds/accounts/default

I'm using ClientLogin auth method and as far as I can see correct Authorization header is sent with each API request (I've used observer class to test for headers which are being sent).

Here is the code I use (stripped-down, test version):

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('/(?:^|[
])Auth=(.*?)(?:[
]|$)/', $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'));

Thanks in advance!

  • 写回答

1条回答 默认 最新

  • dongxing7083 2010-01-22 14:45
    关注

    to answer my own question: https://www.google.com/analytics/feeds/accounts/default must be accessed trough GET method. My code was always using POST.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB动图的问题
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名