如何使用OAuth获取Google Analytics数据?

时间:2022-03-20 14:16:22

Hy guys, we are developing a system which will provide users with access to Google Analytics. I'm trying to implement it in the way so user don't need to enter their Google login credentials on our site, so trying to get it work using their login.

Hy家伙,我们正在开发一个系统,为用户提供访问Google Analytics的权限。我正在尝试以这种方式实现它,因此用户无需在我们的网站上输入他们的Google登录凭据,因此尝试使用他们的登录来使其工作。

I have a solution which gets analytics using user's email and password. I'm looking for a solution which will not require user's email and password but can not find anything.

我有一个解决方案,可以使用用户的电子邮件和密码进行分析。我正在寻找一个解决方案,不需要用户的电子邮件和密码,但找不到任何东西。

How can it be done? any advices or links will be appreciated.

如何做呢?任何建议或链接将不胜感激。

thanks

谢谢

4 个解决方案

#1


11  

Ok, guys, after a few days of struggle I finally figured this out. There is no documentation on the Internet and people who had done it before did not want to share their success by some reason. I found this discussion which helped me.

好吧,伙计们,经过几天的挣扎,我终于弄明白了。互联网上没有文档,以前做过这些文档的人不想出于某种原因分享他们的成功。我发现这个讨论对我很有帮助。

To make it work you will need DotNetOpenAuth from http://www.dotnetopenauth.net/ and gdata from http://code.google.com/p/google-gdata/

要使其工作,您需要来自http://www.dotnetopenauth.net/的DotNetOpenAuth和来自http://code.google.com/p/google-gdata/的gdata。

so

所以

using DotNetOpenAuth.ApplicationBlock;
using DotNetOpenAuth.OAuth;

using Google.GData.Client;
using Google.GData.Analytics;

In DotNetOpenAuth there is sample project named OAuthConsumer which you need. Change it to requiest authorization for Analytics:

在DotNetOpenAuth中,您需要一个名为OAuthConsumer的示例项目。将其更改为对Google Analytics的最高授权:

GoogleConsumer.RequestAuthorization(google, GoogleConsumer.Applications.Analytics);

This will get you Token and Token secret. You can use them like this:

这会让你的Token和Token保密。您可以像这样使用它们:

        GOAuthRequestFactory requestFactory = new GOAuthRequestFactory("cp", TokenManager.ConsumerKey); //ConsumerKey actually is the name of web application
        requestFactory.ConsumerKey = TokenManager.ConsumerKey;
        requestFactory.ConsumerSecret = TokenManager.ConsumerSecret;
        requestFactory.Token = AccessToken;
        requestFactory.TokenSecret = TokenManager.GetTokenSecret(AccessToken);
        requestFactory.UseSSL = true;
        AnalyticsService service = new AnalyticsService(requestFactory.ApplicationName); // acually the same as ConsumerKey
        service.RequestFactory = requestFactory;

        const string dataFeedUrl = "https://www.google.com/analytics/feeds/data";

        DataQuery query1 = new DataQuery(dataFeedUrl);

This service you can use like here or here

您可以在此处或此处使用此服务

And the last thing, you WILL NOT be available to try and test it on localhost so you will need a domain which MUST be registered with Google here in order to get consumer key and secret

最后一件事,你将无法在localhost上尝试测试它,所以你需要一个必须在这里注册Google的域名才能获得消费者密钥和秘密

#2


1  

There's a .NET/C# class for Google Data authentication that can be used to access the Google Analytics Data Export API (since the API is part of the Google Data standard, though you might need to make Google Analytics specific adjustments.)*

有一个用于Google数据身份验证的.NET / C#类可用于访问Google Analytics数据导出API(因为API是Google数据标准的一部分,但您可能需要进行特定于Google Analytics的调整。)*

The authentication is best managed by creating a Google Registered Application, as this allows you to make the authentication without security warnings (and, for that matter, security lapses).

通过创建Google注册应用程序可以最好地管理身份验证,因为这样您就可以在没有安全警告的情况下进行身份验证(并且就此而言,安全性失误)。

There are three forms of supported authentication; the 'secure'/passwordless ones are OAuth and AuthSub (which is the Google-proprietary version of OAuth); the hardcoded username and password version is referred to by Google as 'ClientLogin', and is not considered secure or ideal for multiple-user applications.

支持身份验证有三种形式; '安全'/无密码的是OAuth和AuthSub(这是OAuth的Google专有版本);谷歌将硬编码的用户名和密码版本称为“ClientLogin”,并且不被认为是安全的,也不适合多用户应用程序。

*(Since you tagged the question )

*(因为你标记了问题.netc#)

Edit: More details on using AuthSub or OAuth with the .NET library:

编辑:有关在.NET库中使用AuthSub或OAuth的更多详细信息:

AuthSubSupport: http://code.google.com/p/google-gdata/wiki/AuthSubSupport

AuthSubSupport:http://code.google.com/p/google-gdata/wiki/AuthSubSupport

Code Samples on how to use the libraries for OAuth authentication: http://code.google.com/apis/gdata/docs/auth/oauth.html#2LeggedOAuth (Click the .NET tab).

有关如何使用库进行OAuth身份验证的代码示例:http://code.google.com/apis/gdata/docs/auth/oauth.html#2LeggedOAuth(单击.NET选项卡)。

#3


0  

Basics of working with OAuth are here: http://code.google.com/apis/accounts/docs/OpenID.html#working

使用OAuth的基础知识如下:http://code.google.com/apis/accounts/docs/OpenID.html#working

Authenticating with OAuth: http://code.google.com/apis/accounts/docs/OAuth.html

使用OAuth进行身份验证:http://code.google.com/apis/accounts/docs/OAuth.html

After you authenticate a user with OAuth, you will have the request token that works like the one you get back from Google's login API. From there, it should be the same as username/password.

在使用OAuth对用户进行身份验证后,您将获得与您从Google的登录API中获取的请求令牌相同的请求令牌。从那里,它应该与用户名/密码相同。

#4


-1  

I don't think you need to mess with OAuth.

我不认为你需要弄乱OAuth。

The google analytics api lets you pass credentials. Just start from this data feed example.

谷歌分析API允许您传递凭据。从这个数据Feed示例开始。

http://code.google.com/p/google-gdata/source/browse/trunk/clients/cs/samples/Analytics_DataFeed_Sample/dataFeed.cs

http://code.google.com/p/google-gdata/source/browse/trunk/clients/cs/samples/Analytics_DataFeed_Sample/dataFeed.cs

// Configure GA API and do client login Authorization.
AnalyticsService asv = new AnalyticsService("gaExportAPI_acctSample_v2.0");
asv.setUserCredentials(clientUser, clientPass);

Download the client library here

在此处下载客户端库

http://code.google.com/apis/analytics/docs/gdata/gdataLibraries.html

http://code.google.com/apis/analytics/docs/gdata/gdataLibraries.html

To get a feel for data queries, play with this and then copy the values into the example above

要了解数据查询,请使用此方法,然后将值复制到上面的示例中

http://code.google.com/apis/analytics/docs/gdata/gdataExplorer.html

#1


11  

Ok, guys, after a few days of struggle I finally figured this out. There is no documentation on the Internet and people who had done it before did not want to share their success by some reason. I found this discussion which helped me.

好吧,伙计们,经过几天的挣扎,我终于弄明白了。互联网上没有文档,以前做过这些文档的人不想出于某种原因分享他们的成功。我发现这个讨论对我很有帮助。

To make it work you will need DotNetOpenAuth from http://www.dotnetopenauth.net/ and gdata from http://code.google.com/p/google-gdata/

要使其工作,您需要来自http://www.dotnetopenauth.net/的DotNetOpenAuth和来自http://code.google.com/p/google-gdata/的gdata。

so

所以

using DotNetOpenAuth.ApplicationBlock;
using DotNetOpenAuth.OAuth;

using Google.GData.Client;
using Google.GData.Analytics;

In DotNetOpenAuth there is sample project named OAuthConsumer which you need. Change it to requiest authorization for Analytics:

在DotNetOpenAuth中,您需要一个名为OAuthConsumer的示例项目。将其更改为对Google Analytics的最高授权:

GoogleConsumer.RequestAuthorization(google, GoogleConsumer.Applications.Analytics);

This will get you Token and Token secret. You can use them like this:

这会让你的Token和Token保密。您可以像这样使用它们:

        GOAuthRequestFactory requestFactory = new GOAuthRequestFactory("cp", TokenManager.ConsumerKey); //ConsumerKey actually is the name of web application
        requestFactory.ConsumerKey = TokenManager.ConsumerKey;
        requestFactory.ConsumerSecret = TokenManager.ConsumerSecret;
        requestFactory.Token = AccessToken;
        requestFactory.TokenSecret = TokenManager.GetTokenSecret(AccessToken);
        requestFactory.UseSSL = true;
        AnalyticsService service = new AnalyticsService(requestFactory.ApplicationName); // acually the same as ConsumerKey
        service.RequestFactory = requestFactory;

        const string dataFeedUrl = "https://www.google.com/analytics/feeds/data";

        DataQuery query1 = new DataQuery(dataFeedUrl);

This service you can use like here or here

您可以在此处或此处使用此服务

And the last thing, you WILL NOT be available to try and test it on localhost so you will need a domain which MUST be registered with Google here in order to get consumer key and secret

最后一件事,你将无法在localhost上尝试测试它,所以你需要一个必须在这里注册Google的域名才能获得消费者密钥和秘密

#2


1  

There's a .NET/C# class for Google Data authentication that can be used to access the Google Analytics Data Export API (since the API is part of the Google Data standard, though you might need to make Google Analytics specific adjustments.)*

有一个用于Google数据身份验证的.NET / C#类可用于访问Google Analytics数据导出API(因为API是Google数据标准的一部分,但您可能需要进行特定于Google Analytics的调整。)*

The authentication is best managed by creating a Google Registered Application, as this allows you to make the authentication without security warnings (and, for that matter, security lapses).

通过创建Google注册应用程序可以最好地管理身份验证,因为这样您就可以在没有安全警告的情况下进行身份验证(并且就此而言,安全性失误)。

There are three forms of supported authentication; the 'secure'/passwordless ones are OAuth and AuthSub (which is the Google-proprietary version of OAuth); the hardcoded username and password version is referred to by Google as 'ClientLogin', and is not considered secure or ideal for multiple-user applications.

支持身份验证有三种形式; '安全'/无密码的是OAuth和AuthSub(这是OAuth的Google专有版本);谷歌将硬编码的用户名和密码版本称为“ClientLogin”,并且不被认为是安全的,也不适合多用户应用程序。

*(Since you tagged the question )

*(因为你标记了问题.netc#)

Edit: More details on using AuthSub or OAuth with the .NET library:

编辑:有关在.NET库中使用AuthSub或OAuth的更多详细信息:

AuthSubSupport: http://code.google.com/p/google-gdata/wiki/AuthSubSupport

AuthSubSupport:http://code.google.com/p/google-gdata/wiki/AuthSubSupport

Code Samples on how to use the libraries for OAuth authentication: http://code.google.com/apis/gdata/docs/auth/oauth.html#2LeggedOAuth (Click the .NET tab).

有关如何使用库进行OAuth身份验证的代码示例:http://code.google.com/apis/gdata/docs/auth/oauth.html#2LeggedOAuth(单击.NET选项卡)。

#3


0  

Basics of working with OAuth are here: http://code.google.com/apis/accounts/docs/OpenID.html#working

使用OAuth的基础知识如下:http://code.google.com/apis/accounts/docs/OpenID.html#working

Authenticating with OAuth: http://code.google.com/apis/accounts/docs/OAuth.html

使用OAuth进行身份验证:http://code.google.com/apis/accounts/docs/OAuth.html

After you authenticate a user with OAuth, you will have the request token that works like the one you get back from Google's login API. From there, it should be the same as username/password.

在使用OAuth对用户进行身份验证后,您将获得与您从Google的登录API中获取的请求令牌相同的请求令牌。从那里,它应该与用户名/密码相同。

#4


-1  

I don't think you need to mess with OAuth.

我不认为你需要弄乱OAuth。

The google analytics api lets you pass credentials. Just start from this data feed example.

谷歌分析API允许您传递凭据。从这个数据Feed示例开始。

http://code.google.com/p/google-gdata/source/browse/trunk/clients/cs/samples/Analytics_DataFeed_Sample/dataFeed.cs

http://code.google.com/p/google-gdata/source/browse/trunk/clients/cs/samples/Analytics_DataFeed_Sample/dataFeed.cs

// Configure GA API and do client login Authorization.
AnalyticsService asv = new AnalyticsService("gaExportAPI_acctSample_v2.0");
asv.setUserCredentials(clientUser, clientPass);

Download the client library here

在此处下载客户端库

http://code.google.com/apis/analytics/docs/gdata/gdataLibraries.html

http://code.google.com/apis/analytics/docs/gdata/gdataLibraries.html

To get a feel for data queries, play with this and then copy the values into the example above

要了解数据查询,请使用此方法,然后将值复制到上面的示例中

http://code.google.com/apis/analytics/docs/gdata/gdataExplorer.html