I never post on forums, so I hope that I don't break your code. But I have a question about the capabilities of Zend OAuth.
I tried to use it to get feeds from the Google Analytics API, but I just can't get it to work.
I have an OAuth input that works fine, and I can use it with Google Docs, no problem. But Zend has not come with Google Analytics support yet.
Now I will post my code, and if anyone finds out how to get a feed from Google Analytics using the Zend OAuth functions, I would appreciate it - and there really is not enough information on the Internet on this topic
In addition, I write it as a Wordpress plugin, so ignore all get_option and update_option, etc. Imagine using Session instead :)
Regards, Fredrick
EDIT: Oh, and one more thing. Google Analytics uses this type of URL to retrieve feeds:
https://www.google.com/analytics/feeds/data?ids=ga%3A0000000&metrics=ga%3Apageviews&start-date=2011-05-09&end-date=2011-05- 23 & max-results = 50
$consumerKey = 'XXX';
$secret = 'XXX';
require_once 'Zend/Loader.php';
Zend_Loader::loadClass( 'Zend_Gdata_HttpClient' );
Zend_Loader::loadClass( 'Zend_Gdata_Docs' );
Zend_Loader::loadClass( 'Zend_Gdata_Spreadsheets' );
Zend_Loader::loadClass( 'Zend_Oauth_Consumer' );
Zend_Loader::loadClass( 'Zend_Http_Client' );
Zend_Loader::loadClass( 'Zend_Gdata_Gbase' );
$CONSUMER_KEY = $consumerKey;
$CONSUMER_SECRET = $secret;
$RETURN_TO = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'];
$SCOPES = array(
'https://www.google.com/analytics/feeds/',
);
$oauthOptions = array(
'requestScheme' => Zend_Oauth::REQUEST_SCHEME_HEADER,
'version' => '1.0',
'consumerKey' => $CONSUMER_KEY,
'consumerSecret' => $CONSUMER_SECRET,
'signatureMethod' => 'HMAC-SHA1',
'callbackUrl' => $RETURN_TO,
'requestTokenUrl' => 'https://www.google.com/accounts/OAuthGetRequestToken',
'userAuthorizationUrl' => 'https://www.google.com/accounts/OAuthAuthorizeToken',
'accessTokenUrl' => 'https://www.google.com/accounts/OAuthGetAccessToken'
);
if ( trim( $accessToken ) == '' ) {
$consumer = new Zend_Oauth_Consumer( $oauthOptions );
echo 'yes 1';
update_option( 'ni_trends_google_request_token', serialize( $consumer->getRequestToken( array( 'scope' => implode( ' ', $SCOPES ) ) ) ) );
$approvalUrl = $consumer->getRedirectUrl( array( 'hd' => 'default' ) );
echo '<a href="' . $approvalUrl . '">Grant access</a>';
if ( trim( $accessToken ) == '' ) {
update_option( 'ni_trends_google_access_token', serialize( $consumer->getAccessToken( $_GET, unserialize( $requestToken ) ) ) );
}
update_option( 'ni_trends_google_request_token', '' );
}
$accessToken = unserialize( $accessToken );
$httpClient = $accessToken->getHttpClient($oauthOptions);
$client = new Zend_Gdata_Docs($httpClient, "yourCompany-YourAppName-v1");
$feed = $client->getDocumentListFeed();
echo "<pre>";
echo "<ul>\n";
foreach ($feed->entries as $entry) {
echo "<li>$entry->title </li>\n";
}
echo "</ul>\n";
echo "</pre>\n";
EDIT: Solved it as follows:
$accessToken = unserialize( $accessToken );
$client = $accessToken->getHttpClient( $oauthOptions );
$client->resetParameters();
$parameters = array(
'ids' => 'ga:26870853',
'metrics' => 'ga:pageviews',
'start-date' => '2010-01-08',
'end-date' => '2011-05-22',
'max-results' => '50'
);
$client->setUri('https://www.google.com/analytics/feeds/data');
$client->setParameterGet($parameters);
$client->setMethod(Zend_Http_Client::GET);
$response = $client->request();
print_r( $response );
exit();