0

我正在尝试编写一项服务,该服务将通过 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 低限制的情况下,最简单的方法是什么?

提前非常感谢!

4

2 回答 2

1

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

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

于 2012-04-16T19:23:42.663 回答
0

答案可以在这里找到。

原作者引述:

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

在 DotNetOpenAuth 中,您需要一个名为 OAuthConsumer 的示例项目。将其更改为请求 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);
于 2012-04-16T06:29:48.297 回答