繁体   English   中英

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

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

大家好,我们正在开发一个系统,该系统将为用户提供对Google Analytics(分析)的访问权限。 我正在尝试以某种方式实施它,以便用户无需在我们的网站上输入其Google登录凭据,因此尝试使用其登录名使它正常工作。

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

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

谢谢

好的,伙计们,经过几天的努力,我终于弄明白了。 互联网上没有文档,以前做过此事的人也不想出于某种原因分享自己的成功。 我发现这个讨论对我有所帮助。

要使其正常工作,您将需要http://www.dotnetopenauth.net/中的 DotNetOpenAuthhttp://code.google.com/p/google-gdata/中的 gdata

所以

using DotNetOpenAuth.ApplicationBlock;
using DotNetOpenAuth.OAuth;

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

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

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

这将为您提供令牌和令牌的秘密。 您可以像这样使用它们:

        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);

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

最后一件事,您将无法在localhost对其进行测试,因此您将需要一个必须在此处注册Google的域才能获取使用者密钥和机密

有一个用于Google数据身份验证的.NET / C#类 ,可用于访问Google Analytics(分析)Data Export API(由于该API是Google Data标准的一部分,尽管您可能需要进行Google Analytics(分析)特定的调整。)*

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

支持三种形式的身份验证: “安全” /无密码的是OAuthAuthSubOAuth的Google专有版本); 硬编码的用户名和密码版本被Google称为“ ClientLogin”,并不被认为是安全的,也不是多用户应用程序的理想选择。

*(因为您标记了问题

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

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

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

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

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

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

我认为您不需要弄乱OAuth。

Google Analytics(分析)API可让您传递凭据。 从这个数据提要示例开始。

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);

在此处下载客户端库

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

要体验数据查询,请尝试一下,然后将值复制到上面的示例中

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

暂无
暂无

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

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