Google Prediction API example

[This article was first published on Stats and things, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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…  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…

To leave a comment for the author, please follow the link and comment on their blog: Stats and things. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)