OAuth 2.0与Google Analytics API v3 [英] OAuth 2.0 with Google Analytics API v3

查看:150
本文介绍了OAuth 2.0与Google Analytics API v3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前可以使用我的帐户的登录信息查询Google AnalyticsAPI,密码。
Google现在使用OAuth进行身份验证,这非常棒...
唯一的问题是我只需要一个访问令牌。
我不希望允许其他用户获取他们的分析数据。



我只想获取我的数据。
有没有一种方法可以为我的应用程序或我的分析帐户生成访问令牌?



我知道存在这样的解决方案...例如,Twitter提供了他们称之为单用户oauth的应用程序,不需要特定的用户登录。



再次,我试图完成的一切这里是通过API获取我自己的分析数据。



有没有办法正确地做到这一点?

解决方案

我添加了PHP答案 - 您可以将其调整或转换为garb / ruby​​代码。



您现在应该可以将Google Analytics与服务帐户配合使用。您必须使用私钥而不是访问令牌。



在API控制台中创建应用程序

基本上,您转到Google API控制台并创建应用程序。

在服务选项卡中启用Google Analytics。

在API访问选项卡中,创建一个新的OAuth ID(创建另一个客户端ID ...按钮),选择服务帐户并下载您的私钥(生成新密钥...链接)。您必须稍后将密钥上传到Web服务器。


在API访问页面的服务帐户部分,复制电子邮件地址(@ developer.gserviceaccount.com),并将包含此电子邮件地址的新用户添加到您的Google Analytics配置文件。 如果您不这样做,您会收到一些不错的错误

代码

从SVN下载最新的Google PHP客户端(从命令行 svn checkout http://google-api-php-client.googlecode.com/svn/trunk/ google-api-php-client-您现在可以通过代码访问Analytics API:



$ b

pre> require_once'Google_Client.php';
require_once'contrib / Google_AnalyticsService.php';

$ keyfile ='dsdfdss0sdfsdsdfsdf44923dfs9023-privatekey.p12';

//初始化Google Client对象
$ client = new Google_Client();
$ client-> setApplicationName('您的产品名称');

$ client-> setAssertionCredentials(
new Google_AssertionCredentials(
'11122233344@developer.gserviceaccount.com',
array('https://www.googleapis ($ keyfile)

);

//从Google Console,API访问页面获取此内容
$ client-> setClientId('11122233344.apps.googleusercontent.com');
$ client-> setAccessType('offline_access');
$ analytics =新Google_AnalyticsService($ client);

//我们已完成连接设置,
//现在获取一些数据并输出本周的访问次数。

//您的分析配置文件ID。 (管理员>个人资料设置 - >个人资料ID)
$ analytics_id ='ga:1234';
$ lastWeek = date('Y-m-d',strtotime(' - 1 week'));
$ today = date('Y-m-d');

try {
$ results = $ analytics-> data_ga-> get($ analytics_id,
$ lastWeek,
$ today,'ga:visits' );
echo'< b>本周的访问次数:< / b> ;
echo $ results ['totalsForAllResults'] ['ga:visits'];
} catch(Exception $ e){
echo'有一个错误: - '。 $ E->的getMessage();
}


I used to be able to query the Google Analytics API with my account's login & password. Google is now using OAuth for authentication which is great... The only issue is that I only need ONE access token. I don't wanna allow other users to fetch THEIR analytics data.

I just wanna be able to fetch MY data. Is there a way I can generate an access token only for my app or my analytics account?

I know such solutions exists... For instance, Twitter provides what they call a "single-user oauth" for apps that don't require a specific user to sign in.

One again, all I'm trying to accomplish here is to fetch MY OWN analytics data via the API.

Is there a way to properly do that?

解决方案

I'm adding a PHP answer - you may be able to adjust or convert it to garb / ruby code.

You should be able to use Analytics with service accounts now. You will indeed have to use a private key instead of an access token.

Create an app in the API Console
Basically, you go to the Google API Console and create an App.
Enable Google Analytics in the services tab.
In the API Access tab, create a new OAuth ID (Create another client ID... button), select service account and download your private key (Generate new key... link). You'll have to upload the key to your web server later.

On the API Access page, in the Service account section, copy the email address (@developer.gserviceaccount.com) and add a new user with this email address to your Google Analytics profile. If you do not do this, you'll get some nice errors

Code
Download the latest Google PHP Client off SVN (from the command line svn checkout http://google-api-php-client.googlecode.com/svn/trunk/ google-api-php-client-read-only).

You can now access the Analytics API in code:

require_once 'Google_Client.php';              
require_once 'contrib/Google_AnalyticsService.php';

$keyfile = 'dsdfdss0sdfsdsdfsdf44923dfs9023-privatekey.p12';

// Initialise the Google Client object
$client = new Google_Client();
$client->setApplicationName('Your product name');

$client->setAssertionCredentials(
    new Google_AssertionCredentials(
        '11122233344@developer.gserviceaccount.com',
        array('https://www.googleapis.com/auth/analytics.readonly'),
        file_get_contents($keyfile)
    )
);

// Get this from the Google Console, API Access page
$client->setClientId('11122233344.apps.googleusercontent.com');
$client->setAccessType('offline_access');
$analytics = new Google_AnalyticsService($client);

// We have finished setting up the connection,
// now get some data and output the number of visits this week.

// Your analytics profile id. (Admin -> Profile Settings -> Profile ID)
$analytics_id   = 'ga:1234';
$lastWeek       = date('Y-m-d', strtotime('-1 week'));
$today          = date('Y-m-d');

try {
    $results = $analytics->data_ga->get($analytics_id,
                        $lastWeek,
                        $today,'ga:visits');
    echo '<b>Number of visits this week:</b> ';
    echo $results['totalsForAllResults']['ga:visits'];
} catch(Exception $e) {
    echo 'There was an error : - ' . $e->getMessage();
}

这篇关于OAuth 2.0与Google Analytics API v3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆