Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to authorizate use google-api-ruby-client

i am using google api for ruby, but not know how to start, just give me an ABC example someone, thanks very much?

like image 334
wartalker Avatar asked Sep 21 '11 01:09

wartalker


1 Answers

If you are creating a Service Account application to access Google Analytics.

  1. Register it with Google through https://code.google.com/apis/console. On API Access tab, click Create client ID, choose Service Account. Store the key file Google will generate, and remember the password for that key.
  2. Here is some code to get you started

    require 'rubygems'
    require 'google/api_client'
    
    api_client = Google::APIClient.new
    path_to_key_file ="/path/to/key/file-privatekey.p12"
    passphrase = "google_generated_password"
    key = Google::APIClient::PKCS12.load_key(path_to_key_file, passphrase)
    

Once a key is available, initialize the asserter with your client ID (email in APIs console) and authorization scopes.

asserter = Google::APIClient::JWTAsserter.new(
   'super_long_client_id_from_api_console@developer.gserviceaccount.com',
   'https://www.googleapis.com/auth/analytics.readonly',
   key)

# To request an access token, call authorize:
api_client.authorization = asserter.authorize()
puts api_client.authorization.access_token

http://code.google.com/p/google-api-ruby-client/wiki/ServiceAccounts

like image 69
Ivan Fragoff Avatar answered Sep 26 '22 06:09

Ivan Fragoff