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 

Monday, August 30, 2010

LOTW - 30 Aug

“My husband is a programmer; I have no idea what that means.” - Nice blog post about a non programmer and a programmer geek getting together and learning about each other. I agree with Renae, the important thing is to take an interest in what your other half does during work and his hobbies.

Patterns and Practices for Improving Personal Productivity, Time Management, and Effectiveness

Tim Daly writes about writing big Lisp applications.- Tim Daly responds to Steve Yegge's Lisp is not an acceptable Lisp. I let you make your own decision about whether Lisp is good. For me as a developer/programmer wanting to have more "breath" in your knowledge (I guess most of us are brought up in the OO kind of world), you owe your self to go try one of the functional languages, for me I am going to try Common Lisp and Clojure.

Pascal Costanza's Highly Opinionated Guide to Lisp - Pascal talks about why use Lisp and some of the common pitfalls and where to find more information on your discovery of Lisp. I was always a bit confused by the backquote operator ` in Lisp, his explanation is the easiest I have found. Gotta spend more time reading Practical Common Lisp !


Monday, August 23, 2010

LOTW - 23 Aug 2010

Oracle Sues Google - This must be the big thing this past week.
CNet's Report has a copy of the letter from Oracle's lawyers.
Top 5 grievances with Oracle -  A list of complaints about oracle and open source.
James Gosling foretold
Google saying that Oracle's suit is baseless.


Writing an operating system -  Something that I always wanted to try. Lets put that in the todo list !


F# in Visual Studio - Functional programming is getting lots of press these days, for the JVM; Clojure and Scala and for .Net F#, the video shows how F# can be use most effectively and introduces F# features like parallelism, succinct expressive syntax, rich access to .NET libraries.


You can get the beta Windows 7 Phone developer tools already. Singapore is one of the launch countries so we will have our own application marketplace here.


Scott Hanselman in his 56th Weekly source code talks about the Visual Studio 2010 training kit and some of the cool stuff in there. I was particularly interested in the Parallel Framework




Saturday, August 14, 2010

Oracle sues Google over Android - Possible outcomes.

Oracle finally sues Google over Java. CNET has a copy of the complaint letter from Oracle. Other than a list of patents that was infringed nothing much was said about how it was infringed. I always thought that with the coming of Android, Java finally has a great platform in the mobile arena, I know people will agree with me that Java ME sucks most of the time.

I agree with Miguel that Java as a product has never been profitable for Sun, when Sun was around the big names in the J2EE app server market was Oracle, Bea and probably IBM that made money from selling middleware products. So i guess since Oracle paid quite abit for Sun and so they need to milk it for all it's worth.

How will all this pan out? Below are my 4 guesses.
1. Google pays Oracle
Straight forward way to solve the problem, but that will mean anyone else who has the same idea as Google needs to cough up money big time and that affect's Java, although people might not be moving away quickly but stagnation may occur and that's bad for any language.

2. Google goes to court and wins ! And all is well in the Android world.
Might be a long and drawn out battle, but sets the scene for even more innovation in the Java eco system. The thing is what will Oracle do about Java if it loses this battle.

3. Google goes to court and lose, Google pays Oracle
Same as 1

4. Google moves Android to alternative languages (Python, .Net).
That would be interesting, considering that Goggle is quite the advocate for Java and Python (look at App Engine). Would love to see Android supporting Python in the future, quite like how Symbian has some support for Python (although I think Symbian as a platform is dead also), the next closest alternative would be a move to C#, since they are quite close in terms of syntax and perhaps popularity, and it doesn't cost much for a Java programmer to move to C#.

Monday, August 9, 2010

LOTW - 9 Aug 2010

It's Singapore's National Day today! Happy 45th birthday Singapore!

How to win at Rock Paper Scissors -Not really relevant but it helps you beat your younger nephews and nieces at RPS.

How Yahoo does MySql HA - A blog post by Jay Jessen a MySql Guru at Yahoo.

Seven Open Solaris Home server tips  -  Tips for building an OpenSolaris homeserver with links to other peoples builds. Someone even used an EEEPC as a homeserver. Anyway I thought ZFS was cool stuff, just hope that Oracle can continue with OpenSolaris and start putting out releases. Nexenta looks like a good replacement.

IBM and DBS on the Jul 5th outage - On Jul 5th, DBS atms went down for for a period of almost 7 hours disrupting services, this is the report that attempts to explain why the outage occurs.

Tuesday, August 3, 2010

Learning VIM

I have been an Emacs user most of my life, every since my Polytechnic days when I was introduced to LISP (or the Language Inspired by So many Parenthesis, Yeah i came up with that!).

I decided that finally I was going to learn VIM properly, why VIM since I know emacs? Most of  my free time now is spent on a Netbook specifically the ASUS 1000H, small light netbook with an Atom processor and 2GB of ram. So i wanted a light editor. Anyway I am mostly doing PHP and Python stuff, so an Editor is probably good enough no need for some really fancy IDE.

I am starting a Cookbook for VIM at Google Docs to write down all the VIM commands that I use on a daily basis, perhaps it can serve as a reference for those who are learning VIM but don't know what commands might be useful.

Top books that I recommend for programmers

In selecting the below books, I try not to choose books that are very language specific (no python in 24 hours kind of book) but rather books that I find are general enough to be of use to most programmers.

Head First Design Patterns
Head First Design Patterns
The Head First series of books takes a fun approach to tackling topics, when I first got back in the programming business this was one of the first books i bought to brush up on Design Patterns instead of the dryer Gang of Four book. You get nice diagrams, background stories and illustrations to make a dry topic fun.

Clean Code: a handbook of agile craftsmanship
Clean Code: A Handbook of Agile Software Craftsmanship
Great book by "Uncle Bob" for all software developers, tells you what makes code clean and readable, gives lots of heuristics and guides. Just go read it, you will become better.

Effective Java
Effective Java (2nd Edition)
Not just for the Java Language, contains lots of tips in general for OO languages, but in general tells you how to write better Java code. Joshua Bloch lead the design and implementation of Java Collections Framework.

Ship It!: A practical guide to successful software practices
Ship it! A Practical Guide to Successful Software Projects










From the pragmatic programmers, all developers know how to code (I hope :) ) but what is important is to actually ship the damm thing. This book gives a nice overview of what are the techniques, infrastructure and process that you will need in order to Ship It!. Although the book doesn't really claim to be Agile but most of the techniques they use all come from the Agile philosophy like continuous integration, small iterations, daily meetings.

The Pragmatic Programmer: From Journeyman to Master 
The Pragmatic Programmer: From Journeyman to Master

Pragmatic advice for programmers covers everything from using editors, version control debugging to requirements.  I guess if there is just one book that covers lots of ground it would be this. Everyone should read this, since its not dry and at time humorous.

 Coders at Work
Coders at Work

I don't think that there is another book quite like this. The book contains 15 people who are considered great programmers, the questions for each person are quite standard and repetitive but that's just the beauty of it. You will see how people mostly agree or disagree about a certain topic. Read it so that you will learn a bit about the history of the programing craft since schools don't really teach the history of programming.

What other good books do you recommend?

Monday, August 2, 2010

LOTW - 2 Aug 2010

Microsoft Web Matrix -A package containing IIS Developer Express, SqlServer Compact and a code editor for writing web based apps using ASP.NET, Javascript, HTML and CSS. Small installer, great for anyone i guess for trying Web stuff using MS Technology. From the description it can also code and deploy PHP applications.

Why your customers don't want to talk to you - Interesting read about how customers are increasing going on the self-service route.

Google Tech Talk Node.js: Javascript for the serverPresented by Ryan Dahl, the creator of the node.JS. Node's Goal (as take from the Node.js website) is to provide an easy way to build scalable network programs. It guess what's interesting is it's mainly callback style of programming and the fact that you program in Javascript.


Is good code impossible? - Nice read got it off Uncle Bob, quite a sad state but i guess its mostly true, The other main peeve i have is companies thinking good developers are easily replaceable .