JsonHttpRequestInitializer initializer = new GoogleKeyInitializer("AIzaSyDkFLpg3BcceObNlpDl2odCLYElxj38atU");
Analytics ana = Analytics.builder(new NetHttpTransport(), JSON_FACTORY).setApplicationName("My App1").setJsonHttpRequestInitializer(initializer).build();
It looks like you need to Login too. I can't figure out how to do it in the javaclient api
Also tried the following code with no luck:
DefaultHttpClient client = new DefaultHttpClient();
CookieStore store = new BasicCookieStore();
client.setCookieStore(store);
HttpPost method = new HttpPost("https://www.google.com/accounts/ClientLogin");
method.setHeader("Content-Type", "application/x-www-form-urlencoded");
List<BasicNameValuePair> postParams = new ArrayList<BasicNameValuePair>(4);
postParams.add(new BasicNameValuePair("accountType", "GOOGLE"));
postParams.add(new BasicNameValuePair("Email", "myac...@gmail.com"));
postParams.add(new BasicNameValuePair("Passwd", "gossdevteam"));
postParams.add(new BasicNameValuePair("service", "analytics"));
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParams);
method.setEntity(formEntity);
org.apache.http.HttpResponse responseTmp = client.execute(method);
ByteArrayOutputStream outstream = new ByteArrayOutputStream();
responseTmp.getEntity().writeTo(outstream);
byte [] responseBody = outstream.toByteArray();
String headers = new String(responseBody);
System.out.println("Response Headers: " + headers);
String auth = headers.substring(headers.indexOf("Auth=") + 5);
System.out.println("Auth: " + auth);
org.apache.http.impl.client.DefaultHttpClient OAuthClient = new DefaultHttpClient();
CookieStore OAuthCookieStore = new BasicCookieStore();
ResponseHandler<byte[]> handlerOAuth = new ResponseHandler<byte[]>() {
public byte[] handleResponse(org.apache.http.HttpResponse response) throws IOException {
HttpEntity entity = response.getEntity();
if (entity != null) {
return EntityUtils.toByteArray(entity);
} else {
return null;
}
}
};
OAuthClient.setRedirectHandler(new DefaultRedirectHandler() {
public boolean isRedirected(org.apache.http.HttpRequest request, org.apache.http.HttpResponse response) {
boolean isRedirect=false;
if (!isRedirect) {
int responseCode = response.getStatusLine().getStatusCode();
if (responseCode == 301 || responseCode == 302) {
return true;
}
}
return false;
}
});
OAuthClient.setCookieStore(OAuthCookieStore);
String CLIENT_ID = "xxxx.apps.googleusercontent.com";
HttpPost OAuthPost = new HttpPost("https://accounts.google.com/o/oauth2/auth");
List<NameValuePair> parOAuth = new ArrayList<NameValuePair>(6);
parOAuth.add(new BasicNameValuePair("client_id", CLIENT_ID));
parOAuth.add(new BasicNameValuePair("redirect_uri", "https://oauth2-login-demo.appspot.com/code"));
parOAuth.add(new BasicNameValuePair("response_type", "code"));
parOAuth.add(new BasicNameValuePair("scope", "https://www.googleapis.com/auth/analytics.readonly"));
OAuthPost.setEntity(new UrlEncodedFormEntity(parOAuth));
byte[] OAuthResponse = OAuthClient.execute(OAuthPost, handlerOAuth);
What about Management api?