简体   繁体   English

如何使用OAuth获取Google Analytics(分析)数据?

[英]How to get Google Analytics data using OAuth?

Hy guys, we are developing a system which will provide users with access to Google Analytics. 大家好,我们正在开发一个系统,该系统将为用户提供对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. 我正在尝试以某种方式实施它,以便用户无需在我们的网站上输入其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 谢谢

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/中的 DotNetOpenAuthhttp://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. 在DotNetOpenAuth中,您需要一个名为OAuthConsumer的示例项目。 Change it to requiest authorization for Analytics: 将其更改为对Google Analytics(分析)要求的授权:

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

This will get you Token and Token secret. 这将为您提供令牌和令牌的秘密。 You can use them like this: 您可以像这样使用它们:

        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的域才能获取使用者密钥和机密

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(分析)Data Export API(由于该API是Google Data标准的一部分,尽管您可能需要进行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); “安全” /无密码的是OAuthAuthSubOAuth的Google专有版本); the hardcoded username and password version is referred to by Google as 'ClientLogin', and is not considered secure or ideal for multiple-user applications. 硬编码的用户名和密码版本被Google称为“ ClientLogin”,并不被认为是安全的,也不是多用户应用程序的理想选择。

*(Since you tagged the question ) *(因为您标记了问题

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选项卡)。

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. 使用OAuth对用户进行身份验证后,您将获得请求令牌,该令牌的工作方式与您从Google的登录API中获得的令牌相同。 From there, it should be the same as username/password. 从那里开始,它应该与用户名/密码相同。

I don't think you need to mess with OAuth. 我认为您不需要弄乱OAuth。

The google analytics api lets you pass credentials. Google Analytics(分析)API可让您传递凭据。 Just start from this data feed example. 从这个数据提要示例开始。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM