Hi,
I do get a credential, but unfortunately I have an open question about getting and using a refresh token. I cannot seem to determine how much time I have left (it's always null).
I'm using a service account, so what you see below reflects that.
private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
private static final JsonFactory JSON_FACTORY = new JacksonFactory();
private static final String SERVICE_ACCOUNT_EMAIL = "some-email-address";
private static final String PKI_FILE = ".some-file";
private static Analytics initializeAnalytics() throws Exception {
if (log.isInfoEnabled()) {
log.info("Creating Google Analytics Service Object");
}
// Authorization.
File pki = new File(PKI_FILE);
if (!pki.canRead()) {
if (log.isErrorEnabled()) {
log.error("Cannot read file");
}
} else {
if (log.isInfoEnabled()) {
log.info("File available at: " + PKI_FILE);
}
}
GoogleCredential credential = new GoogleCredential.Builder().setTransport(HTTP_TRANSPORT)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
.setServiceAccountScopes(AnalyticsScopes.ANALYTICS_READONLY)
// .setServiceAccountPrivateKeyFromP12File(pki)
.setServiceAccountPrivateKeyFromP12File(new File(App.class.getResource("somewhere-in-the-classpath").toURI()))
.build();
// Set up and return Google Analytics API client.
return new Analytics.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
.setApplicationName("NGAnalytics")
.build();
}
You'll need to get a private key, and a service account email address. This is basically what's posted in Mercurial, with some logging and reading the PK file out of a JAR file.
This works fine on the command line.
Once you get the Analytics object, you can get a profile ID, and then do the query. The code from Mercurial works as is. One big help is working from Maven.
I'm now having fun lacing this all together with JSF 2 / Primefaces, and MySQL for some post-processing.
Mark
/mde/