2013-03-19 66 views
2

我正在尝试创建仪表板,包括显示来自Google Analytics的一些分析报告。 搜索后,我发现下面的SO话题:Google Analytics authorization in java获取配置文件时Google Analytics API证书异常

在其中一个答案,我发现下面的服务帐户例如:

private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport(); 
private static final JsonFactory JSON_FACTORY = new JacksonFactory(); 

public static Result index() { 
    GoogleCredential credential = null; 
    try { 
     credential = new GoogleCredential.Builder().setTransport(HTTP_TRANSPORT) 
       .setJsonFactory(JSON_FACTORY) 
       .setServiceAccountId("[email protected]") 
       .setServiceAccountScopes(AnalyticsScopes.ANALYTICS_READONLY) 
       .setServiceAccountPrivateKeyFromP12File(new File("/your/path/to/privatekey/privatekey.p12"))       
       .build(); 
    } catch (GeneralSecurityException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    // Set up and return Google Analytics API client. 
    Analytics analytics = new Analytics.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential).setApplicationName(
      "Google-Analytics-Hello-Analytics-API-Sample").build(); 

    String profileId = ""; 
    try { 
     profileId = getFirstProfileId(analytics); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    GaData gaData = null; 
    try { 
     gaData = executeDataQuery(analytics, profileId); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    printGaData(gaData); 

    return ok(index.render("Your new application is ready.")); 
} 

private static String getFirstProfileId(Analytics analytics) throws IOException { 
    String profileId = null; 

    // Query accounts collection. 
    Accounts accounts = analytics.management().accounts().list().execute(); 

    if (accounts.getItems().isEmpty()) { 
     System.err.println("No accounts found"); 
    } else { 
     String firstAccountId = accounts.getItems().get(0).getId(); 

     // Query webproperties collection. 
     Webproperties webproperties = 
       analytics.management().webproperties().list(firstAccountId).execute(); 

     if (webproperties.getItems().isEmpty()) { 
      System.err.println("No Webproperties found"); 
     } else { 
      String firstWebpropertyId = webproperties.getItems().get(0).getId(); 

      // Query profiles collection. 
      Profiles profiles = 
        analytics.management().profiles().list(firstAccountId, firstWebpropertyId).execute(); 

      if (profiles.getItems().isEmpty()) { 
       System.err.println("No profiles found"); 
      } else { 
       profileId = profiles.getItems().get(0).getId(); 
      } 
     } 
    } 
    return profileId; 
} 

/** 
* Returns the top 25 organic search keywords and traffic source by visits. The Core Reporting API 
* is used to retrieve this data. 
* 
* @param analytics the analytics service object used to access the API. 
* @param profileId the profile ID from which to retrieve data. 
* @return the response from the API. 
* @throws IOException tf an API error occured. 
*/ 
private static GaData executeDataQuery(Analytics analytics, String profileId) throws IOException { 
    return analytics.data().ga().get("ga:" + profileId, // Table Id. ga: + profile id. 
      "2012-01-01", // Start date. 
      "2012-01-14", // End date. 
      "ga:visits") // Metrics. 
      .setDimensions("ga:source,ga:keyword") 
      .setSort("-ga:visits,ga:source") 
      .setFilters("ga:medium==organic") 
      .setMaxResults(25) 
      .execute(); 
} 

/** 
* Prints the output from the Core Reporting API. The profile name is printed along with each 
* column name and all the data in the rows. 
* 
* @param results data returned from the Core Reporting API. 
*/ 
private static void printGaData(GaData results) { 
    System.out.println("printing results for profile: " + results.getProfileInfo().getProfileName()); 

    if (results.getRows() == null || results.getRows().isEmpty()) { 
     System.out.println("No results Found."); 
    } else { 

     // Print column headers. 
     for (GaData.ColumnHeaders header : results.getColumnHeaders()) { 
      System.out.printf("%30s", header.getName()); 
     } 
     System.out.println(); 

     // Print actual data. 
     for (List<String> row : results.getRows()) { 
      for (String column : row) { 
       System.out.printf("%30s", column); 
      } 
      System.out.println(); 
     } 

     System.out.println(); 
    } 
} 

不幸的是,我似乎无法使它工作..我结束了以下异常:

Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target 
at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:385) 
at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:292) 
at sun.security.validator.Validator.validate(Validator.java:260) 
at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:326) 
at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:231) 
at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:126) 
at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1320) 
... 175 more 
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target 
at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:196) 
at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:268) 
at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:380) 
... 181 more 

发生异常时一个下面一行:

analytics.management().accounts().list().execute(); 

我敢肯定,我用正确的配置和设置生成的私钥正确的道路..

当使用OAuth园地(https://developers.google.com/oauthplayground/),但人看起来很简单:

  • 授权应用
  • 交易所授权码“刷新”和“访问”令牌

但发现文档是所有的地方,而不是很清楚(我无论如何)..

任何想法如何解决这个问题?任何帮助深表感谢!

回答

1

有关此类问题,请参阅previous answer。这看起来是一个SSL /信任存储问题,而不是OAuth的任何事情。

+0

这确实是一个信托商店的问题。我使用InstallCert导入了所需的证书(请参阅:http://stackoverflow.com/questions/3685548/java-keytool-easy-way-to-add-server-cert-from-url-port)。作为我使用的主机,https://www.googleapis.com。之后,我有所有需要的证书,并解决例外:)。谢谢! – NickGreen 2013-03-22 08:39:31