Thursday, June 13, 2013

Google Prediction API example

Google's prediction API offers a blackbox way of doing some prediction. They had advertised an R package, but it doesn't seem to work with the new version of the prediction API or their OAuth2 authentication mechanism. So, in an effort to check out the prediction API, I tried to go through the authentication process myself. I got it to work, but couldn't get it ported to RCurl successfully, so if you have any suggestions, I'd love to hear them. Since I was unable to get RCurl working, I am using a system() call with 'curl' that is installed on my machine.

The first step is to fill out your API keys and all of that sort of stuff. Note that this must be done in strict accordance with Google's rules. The scope must match what you've entered on the google auth page, same with the redirect_uri. 

Then, use those things to submit a request for an authorization code. You will use this code later to request an authorization token. browseURL() opens a page in your browser window. Log into google (if you aren't already) and look at the resulting URL, it contains your code. Example response would be like this... https://oauth2-login-demo.appspot.com/code?state=/profile&code=4/P7q7W91a-oMsCeLvIaQm6bTrgtp7  You want everything after '&code='

Okay, if successful, you now have an authorization code. Cut and paste that from your browser into the your R code and continue on with a authorization token request.

Now, if you are successful, 'token_code' contains what you need to make a request to the google prediction API. Here's how you try it out on a demo language guessing model.

You should end up with a response like this

All of the code together... https://gist.github.com/corynissen/5774784

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.