How to get google analytics data using java?

2019-09-10 09:08发布

问题:

I am trying to download a google analytics data using java. I am using a quick start code provided by google developer guide. I downloaded oauth credential file in json format from the developer console. I also given the json path to program but I am getting error. Please help

public class HelloAnalytics {
  // Path to client_secrets.json file downloaded from the Developer's Console.
  // The path is relative to HelloAnalytics.java.
  private static final String CLIENT_SECRET_JSON_RESOURCE = "ga-data-report-7ac3499541bf.json";

  // The directory where the user's credentials will be stored.
  private static final File DATA_STORE_DIR = new File(
      System.getProperty("user.home"), "store/hello_analytics");

  private static final String APPLICATION_NAME = "Hello Analytics";
  private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();
  private static NetHttpTransport httpTransport;
  private static FileDataStoreFactory dataStoreFactory;

  public static void main(String[] args) {
    try {
      Analytics analytics = initializeAnalytics();
      String profile = getFirstProfileId(analytics);
      printResults(getResults(analytics, profile));
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

回答1:

I think the answer is on my post: Hello Analytics API: Java quickstart errors

You can check out my code there aswel. I Don't just check the first profile of the first webproperty of the first account. I check them all, cause more is better.