开发者

"Target feed is read-only" in Google Analytics API using PEAR HTTP_Request2

开发者 https://www.devze.com 2022-12-17 14:32 出处:网络
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 f

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('/(?:^|[\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 f开发者_Go百科unction 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!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消