Created
June 13, 2013 15:46
-
-
Save corynissen/5774784 to your computer and use it in GitHub Desktop.
google prediction api example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(rjson) | |
library(RCurl) | |
client_id <- "client id here" | |
client_secret <- "client secret here" | |
redirect_uri <- "https://localhost/oauth2callback" | |
auth_url <- "https://accounts.google.com/o/oauth2/auth" | |
scope <- "https://www.googleapis.com/auth/prediction" | |
api_key <- "api key here" | |
code_url <- paste0(auth_url, "?client_id=", client_id, "&redirect_uri=", | |
redirect_uri, "&response_type=code&scope=", scope) | |
# this should open a browser and ask you to login to google. Then it will | |
# redirect you to a "localhost" page, which doesn't exist, | |
# copy that code in the url... | |
browseURL(code_url) | |
# cust and paste that code here... | |
code <- "paste your code from the last step here" | |
# use code to get token_code | |
token.curl.request <- paste0('curl -k --header "Content-Type: application/x-www-form-urlencoded" --data "code=', | |
code, '&client_id=', client_id, '&client_secret=', | |
client_secret, '&redirect_uri=', redirect_uri, | |
'&grant_type=authorization_code', | |
'" https://accounts.google.com/o/oauth2/token') | |
token <- system(token.curl.request, intern=T) | |
token_code <- fromJSON(paste(token, collapse=""))$access_token | |
# use token_code | |
hosted.model <- "sample.languageid" | |
model.url <- "https://www.googleapis.com/prediction/v1.5/hostedmodels/" | |
model.curl.request <- paste0('curl -k --header "Content-Type: application/json" --data \'{"input":{"csvInstance":["Como se llama?"]}}\' -H "Authorization: Bearer ', token_code, '" https://www.googleapis.com/prediction/v1.5/hostedmodels/sample.languageid/predict?key=', api_key, " --verbose") | |
system(model.curl.request, intern=T) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment