Java中的Google Analytics:分析服务“应用程序名称”

时间:2014-02-14 11:39:19

标签: java google-analytics google-analytics-api

Java新手在这里。我正在尝试实现一个从GoogleAnalytics收集数据的简单Java应用程序。假设我的包名是“gDataExample”,我访问GA的用户名是“example@gmail.com”,我的密码是“password”,我的表ID是“ga:my_table_id”我的代码到现在为止。

public class AnalyticsMain {

  // Credentials for Client Login Authorization.
  private static final String CLIENT_USERNAME = "example@gmail.com";
  private static final String CLIENT_PASS = "password";

  // Table ID constant
  private static final String TABLE_ID = "ga:my_table_id";

  public static void main(String args[]) {
    try {
      // Service Object to work with the Google Analytics Data Export API.
      AnalyticsService analyticsService = new AnalyticsService("gDataExample");

      // Client Login Authorization.
      analyticsService.setUserCredentials(CLIENT_USERNAME, CLIENT_PASS);

      // Get data from the Account Feed.
     // getAccountFeed(analyticsService);

    } catch (AuthenticationException e) {
      System.err.println("Authentication failed : " + e.getMessage());
      return;
    }
}

然而,当我运行它时,我收到以下错误:

Exception in thread "main" java.lang.NoSuchMethodError: com.google.common.collect.ImmutableSet.copyOf([Ljava/lang/Object;)Lcom/google/common/collect/ImmutableSet;
    at com.google.gdata.wireformats.AltFormat$Builder.setAcceptableTypes(AltFormat.java:399)
    at com.google.gdata.wireformats.AltFormat$Builder.setAcceptableXmlTypes(AltFormat.java:387)
    at com.google.gdata.wireformats.AltFormat.<clinit>(AltFormat.java:49)
    at com.google.gdata.client.Service.<clinit>(Service.java:558)
    at AnalyticsMain.main(AnalyticsMain.java:34)

第34行是

AnalyticsService analyticsService = new AnalyticsService("gDataExample");

所以这是我的问题:“我的应用程序的名称”必须作为“新的AnalyticsService()”的参数提供什么?

1 个答案:

答案 0 :(得分:0)

据我所知,您无法将client login(setUserCredentials)与Google Analytics结合使用。您需要/应该使用Oauth2连接到Google Analytics API

Google Oauth2

Hello Analtyics java Tutorial