FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Authentication code creation for google analytics API in PHP
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: Authentication code creation for google analytics API in PHP [message #169762 is a reply to message #169737] Sat, 25 September 2010 04:48 Go to previous messageGo to previous message
neha rathi is currently offline  neha rathi
Messages: 4
Registered: September 2010
Karma:
Junior Member
Hi,

I am not getting any problem in installing Analytics code in websites.
I am trying a code in which I am collecting daily visits of all
domains of my google analytics account on one page.
It requires a google analytics API. In which it requires google
authentication code. my doubt is how to get it.
Without that code I am getting all visits on local xampp server but
not on dedicated server.

It gives me error as


Fatal error: Uncaught exception 'Exception' with message 'Permission
Denied - <HTML> <HEAD> <TITLE>Token invalid</TITLE> </HEAD> <BODY
BGCOLOR="#FFFFFF" TEXT="#000000"> <H1>Token invalid</H1> <H2>Error
401</H2> </BODY> </HTML> ' in /home/gauravhm/public_html/seo/admin/
domain_add/googleanalytics.class.php:266 Stack trace: #0 /home/
gauravhm/public_html/seo/admin/domain_add/
googleanalytics.class.php(195): GoogleAnalytics->_postTo('https://
www.goo...', Array, Array) #1 /home/gauravhm/public_html/seo/admin/
domain_add/googleanalytics.class.php(116): GoogleAnalytics-
> _callAPI('https://www.goo...') #2 /home/gauravhm/public_html/seo/
admin/domain_add/visits.php(76): GoogleAnalytics->getReport(Array) #3
{main} thrown in /home/gauravhm/public_html/seo/admin/domain_add/
googleanalytics.class.php on line 266


I am using the code snippet as


private function _callAPI($url)
{
return $this->_postTo($url,array(),array("Authorization:
GoogleLogin auth=".$this->_authCode));
}

/**
* Authenticate the email and password with Google, and set the
$_authCode return by Google
*
* @param none
* @return none
*/
private function _authenticate() {
$postdata = array(
'accountType' => 'GOOGLE',
'Email' => $this->_email,
'Passwd' => $this->_passwd,
'service' => 'analytics',
'source' => 'askaboutphp-v01'
);

$response = $this->_postTo("https://www.google.com/accounts/
ClientLogin", $postdata);
//process the response;
if ($response) {
preg_match('/Auth=(.*)/', $response, $matches);
if(isset($matches[1])) {
$this->_authCode = $matches[1];
return TRUE;
}
}
return FALSE;
}

/**
* Performs the curl calls to the $url specified.
*
* @param string $url
* @param array $data - specify the data to be 'POST'ed to $url
* @param array $header - specify any header information
* @return $response from submission to $url
*/
private function _postTo($url, $data=array(), $header=array()) {

//check that the url is provided
if (!isset($url)) {
return FALSE;
}

//send the data by curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
if (count($data)>0) {
//POST METHOD
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
} else {
$header[] = array("application/x-www-form-urlencoded");
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
}

$response = curl_exec($ch);
$info = curl_getinfo($ch);

curl_close($ch);

//print_r($info);
//print $response;
if($info['http_code'] == 200) {
return $response;
} elseif ($info['http_code'] == 400) {
throw new Exception('Bad request - '.$response);
} elseif ($info['http_code'] == 401) {
throw new Exception('Permission Denied - '.$response);
} else {
return FALSE;
}

}


Please help.
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Previous Topic: Are there C libraries that are comparable to the PHP libraries?
Next Topic: Capture logoff on browser close
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sun Dec 01 03:38:02 GMT 2024

Total time taken to generate the page: 0.03626 seconds