【问题标题】:Google Analytics Data from Console App with C#使用 C# 来自控制台应用程序的 Google Analytics(分析)数据
【发布时间】:2012-04-17 03:23:42
【问题描述】:

我正在尝试编写一项服务,该服务将通过 Windows 服务或控制台应用程序提取 Google Analytics(分析)数据。

无论我尝试什么,我都无法使用 oAuth 授权我的应用

我可以做这样的事情

 var analyticsService = new AnalyticsService("MyApp");
 const string baseUrl = "https://www.google.com/analytics/feeds/data";

 var dataQuery = new DataQuery(baseUrl);

 dataQuery.Ids = TableId;
 dataQuery.Dimensions = "ga:pagePath,ga:date";
 dataQuery.Metrics = "ga:avgTimeOnPage,ga:pageviews,ga:uniquePageviews";
 dataQuery.Sort = "ga:date";
 dataQuery.GAStartDate = "2012-03-01";
 dataQuery.GAEndDate = "2012-04-15";

 Feed = analyticsService.Query(dataQuery);

如果我将 GDataCredentials 与我的帐户用户名/密码一起使用,这可以正常工作。我的印象是,这每天只给我 200 个请求。我真的需要一些示例代码的帮助,以使其与 oAuth 一起使用以允许 50K 请求。我在这里完全撞墙了。

只是为了澄清:

我的网站只有一个 GoogleAnalytics 帐户。我将永远从那个帐户中提取。在不达到 API 低限的情况下,最简单的方法是什么?

提前非常感谢!

【问题讨论】:

  • 不是重复的,因为该答案不再适用于开箱即用

标签: c# dotnetopenauth google-analytics-api


【解决方案1】:

经过进一步研究,我正在寻找的选项似乎是服务帐户,它对 Google Analytics(分析)不可用(还没有?)

https://developers.google.com/accounts/docs/OAuth2ServiceAccount

【讨论】:

    【解决方案2】:

    答案可以在here.找到

    原作者引述:

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

    在 DotNetOpenAuth 中有一个名为 OAuthConsumer 的示例项目,它 你需要。将其更改为请求分析授权:

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

    【讨论】:

    • 对,我的问题是我试过了,但它不起作用:) 它缺少各种引用,API 已更改,库版本等。你能提供一个大样本吗?
    猜你喜欢
    • 2011-10-09
    • 2013-05-29
    • 1970-01-01
    • 2012-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-20
    相关资源
    最近更新 更多