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

Bur*_*jua 9 .net c# google-analytics oauth authsub

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

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

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

谢谢

Bur*_*jua 11

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

为了使它工作,你需要DotNetOpenAuthhttp://www.dotnetopenauth.net/gdatahttp://code.google.com/p/google-gdata/

所以

using DotNetOpenAuth.ApplicationBlock;
using DotNetOpenAuth.OAuth;

using Google.GData.Client;
using Google.GData.Analytics;
Run Code Online (Sandbox Code Playgroud)

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

GoogleConsumer.RequestAuthorization(google, GoogleConsumer.Applications.Analytics);
Run Code Online (Sandbox Code Playgroud)

这将使您获得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);
Run Code Online (Sandbox Code Playgroud)

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

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