2

I am trying to simply send a request to google's hosted model sample.sentiment. I cannot figure out how to authorize with Oauth 2.0 via google, and I am getting endless amounts of hours. If you could provide me the code to this is would be helpful. Here is what I am working off of.

client = Google::APIClient.new({:application_name => "CCE",:application_version => "1.0"} )
plus = client.discovered_api('prediction')

# Initialize OAuth 2.0 client    
client.authorization.client_id = 'my client id'
client.authorization.client_secret = 'my client secret'
client.authorization.redirect_uri = 'my callback url'

client.authorization.scope = 'https://www.googleapis.com/auth/prediction'

# Request authorization
redirect_uri = client.authorization.authorization_uri

# Wait for authorization code then exchange for token
client.authorization.code = '....'
client.authorization.fetch_access_token!

# Make an API call
 result = client.execute(
   :api_method => plus.activities.list,
   :parameters => {'hostedModelName' => 'sample.sentiment', 'userId' => ''})

`

Eric Walker
  • 7,063
  • 3
  • 35
  • 38
Robbie Guilfoyle
  • 3,363
  • 1
  • 18
  • 18

1 Answers1

6

Well, the examples on the web may be a little confusing, but if you want to utilize a server-server communication - meaning that no end-user and no browser is involved, then the following code should work for you:

The prerequisites:

  • you should generate a "service account" key in your Google API Console. It will prompt you to download a private key, which you should store somewhere on your disk, for example as client.p12 (or use the original name, but for clarity I'll use the shorter one).
client = Google::APIClient.new(
  :application_name => "CCE",
  :application_version => "1.0"
)
prediction = client.discovered_api('prediction', 'v1.5')

key = Google::APIClient::KeyUtils.load_from_pkcs12('client.p12', 'notasecret')

client.authorization = Signet::OAuth2::Client.new(
  :token_credential_uri => 'https://accounts.google.com/o/oauth2/token',
  :audience => 'https://accounts.google.com/o/oauth2/token',
  :scope => 'https://www.googleapis.com/auth/prediction',
  :issuer => '..put here your developer email address from Google API Console..',
  :signing_key => key,
)
client.authorization.fetch_access_token!

# Now you can make the API calls
result = client.execute(...

Worth noting is that client.discovered_api call seems to require the version number. otherwise it may be throwing an exception 'NotFound'.

The passphrase is really the string 'notasecret'!

Another thing: in API call make sure that you call the proper method - for hosted models I believe the only method you can call is :api_method => prediction.hostedmodels.predict or something like this. I've not used hosted models yet. (see the API docs for details)

The possibly interesting fields of the result returned by the client.execute call are:

result.status
result.data['error']['errors'].map{|e| e['message']} # if they exist
JSON.parse(result.body)

If you inspect them, they will probably help you significantly in debugging any problems.

Arsen7
  • 12,522
  • 2
  • 43
  • 60
  • 1
    This was very helpful for me for getting started. Just to clarify for other people -- there are two kinds of accounts involved here; "service" accounts, which authenticate with a private key, and "Web" accounts, which authenticate with a callback hook. The OP presents authentication using callback, and this answer uses a service account, with the private key. Theoretically it should be possible to get things going with the Web account and callback as well. – Eric Walker Nov 08 '13 at 23:18