Tuesday, August 31, 2010

Exploring Google Analytics Api - 1 Client Login

Blogger dosen't have a "popular posts" widget, and since I am using Google Analytics and there is a popluar posts metric, i decided to roll my own and host it using App Engine. I am roughly following the examples on the Google Analytics Api site but adapting it for AppEngine use.

There are 3 methods to authenticate to Google Analytics and retrieving data.
1. Client Login
2. AuthSub
3. OAuth

The explanation for all these 3 types are provided here with the various scenarios on what to use.

If you are planning to serve out data from a server you should use OAuth, its the most secure and there is no passing of passwords anywhere, however since I am still playing around with the API and I don't want to mess with the complexities of OAuth authentication. I will be using ClientLogin for now.

Google does provide client libraries in Java for accessing the Google Data API (which Analytics is a part of), however they don't play well with App Engine because they use some restricted classes. So we will have to go with the protocol version, i.e using POST, GET calls to get the data.

You will need to POST the following data to https://www.google.com/accounts/ClientLogin with the following parameters.The table below from Google shows the parameters.

accountTypeType of account to request authorization for. The default is GOOGLE, which is currently the only option supported by Google Analytics.
EmailThe user's email address. It must include the domain (e.g. joe@gmail.com).
PasswdThe user's password.
serviceThe Analytics service name is analytics. (For other service names, see the service name list.)
sourceA string identifying your client application in the form companyName-applicationName-versionID.

Table 1: Post Parameters (Credits Google)

In App Engine, to do POST/GET request you will have to go quite low level, you will need to use the URL and HttpURLConnection classes. Below is a sample of using the classes to do a POST to the client URL.

URL loginUrl=new URL("https://www.google.com/accounts/ClientLogin");
   
StringBuffer message=new StringBuffer();
message.append("accountType=GOOGLE&");
message.append("Email="+URLEncoder.encode(username)+"&");
message.append("Passwd="+URLEncoder.encode(password)+"&");
message.append("service=analytics&");
message.append("source=stephenonsoftware-sitedata-1");
   
HttpURLConnection connection=(HttpURLConnection) loginUrl.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
   
OutputStreamWriter writer=new OutputStreamWriter(connection.getOutputStream());
writer.write(message.toString());
   
writer.close();
   
if(connection.getResponseCode()==HttpURLConnection.HTTP_OK){
BufferedReader reader=new BufferedReader(new InputStreamReader(connection.getInputStream()));
    
String line;
StringBuffer result=new StringBuffer();
while((line=reader.readLine())!=null){
 result.append(line);
}
reader.close();
return getAuthString(result.toString());
}else{
return "";
}

You will get back a long string containg the SID,LSID and Auth parts, you will just need the Auth part so just split the string at 'Auth=' and you can get the authentication token, you will need this token to request for the data from Analytics.

Now you have successfully login using ClientLogin, next I will post about how to get your account details from analytics.

Links:
Using java.net 

No comments:

Post a Comment