如何将 Google Analytics API 与 Zend OAuth 结合使用?
我从来没有在论坛上写过文章,所以我希望我不会破坏你的法典。但我对 Zend 的 OAuth 功能有疑问。
我一直在尝试使用它从 Google Analytics API 获取提要,但我就是无法让它工作。
我的 OAuth 登录工作正常,我可以将它与 Google Docs 一起使用,没有问题。但 Zend 尚未支持 Google Analytics。
我现在要发布我的代码,如果有人知道如何使用 Zend 的 OAuth 功能从 Google Analytics 获取提要,我将不胜感激 - 互联网确实缺乏有关此主题的信息!
另外,我将其编写为 WordPress 插件,因此请忽略所有 get_option 和 update_option 等。想象一下您正在使用 Sessions :)
问候, Fredrik
编辑:哦,还有一件事。 Google Analytics 使用这种 URL 来获取 feed: 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' );
// set your Google consumer key / secret
$CONSUMER_KEY = $consumerKey;
$CONSUMER_SECRET = $secret;
$RETURN_TO = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'];
// Multi-scoped token.
// Sorry! I'm using the analytics scope at this particular place in the code. In the bottom of this code though, there's some request code that needs the Google Docs scope. If you want a working example for Google Docs, change this scope accordingly.
$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 );
// This is where I run into trouble. This is for Google Docs, and it's working (although I have the Analytics Scope configured at the moment) - but how do I make my request and fetch the feed from Google Analytics?
$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";
编辑:用以下方法解决了它!
$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();
I never write in forums, so I hope I don't break your codex. But I have a question regarding Zend's OAuth features.
I've been trying to use it to get feeds from Google Analytics API, but I just can't get it to work.
I've got the OAuth sign-in working just fine, and I can use it with i.e. Google Docs, no problem. But Zend doesn't yet come with support for Google Analytics.
I'm going to post my code now, and if anyone has a clue about how to get a feed out of Google Analytics using Zend's OAuth features, I'd appreciate it - and the internet is really lacking info on this topic!
Also, I'm writing it as a Wordpress plugin, so disregard all the get_option and update_option etc. Imagine you're using Sessions instead :)
Regards,
Fredrik
EDIT: Oh, and one more thing. Google Analytics uses this kind of URL to get 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' );
// set your Google consumer key / secret
$CONSUMER_KEY = $consumerKey;
$CONSUMER_SECRET = $secret;
$RETURN_TO = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'];
// Multi-scoped token.
// Sorry! I'm using the analytics scope at this particular place in the code. In the bottom of this code though, there's some request code that needs the Google Docs scope. If you want a working example for Google Docs, change this scope accordingly.
$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 );
// This is where I run into trouble. This is for Google Docs, and it's working (although I have the Analytics Scope configured at the moment) - but how do I make my request and fetch the feed from Google Analytics?
$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 with the following!
$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();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个,
https://github.com/danielmitd/Zend_Gdata_Analytics/tree/master/ Zend/Gdata
它不是官方的,但它非常适合我。原始页面似乎已经关闭,所以我不确定追踪文档有多难。如果需要的话我可以发布一些例子。
Try this,
https://github.com/danielmitd/Zend_Gdata_Analytics/tree/master/Zend/Gdata
It's not official, but it works perfectly for me. The original page is down it seems, so I'm not sure how hard the documentation will be to track down. I can post some examples if needed.