How to authorize use of google-api-ruby-client

I am using google api for ruby ​​but don’t know how to start, just give me an ABC example, thank you very much?

+6
source share
2 answers

If you are creating a service account application to access Google Analytics.

  • Register it with Google through https://code.google.com/apis/console . On the "API Access" tab, click "Create Client ID", select "Service Account." Save the key file that will be created by Google and remember the password for this key.
  • Below is the 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 the key is available, initialize the asserter with your client identifier (email in the API console) and authority.

 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

+11
source

I answered something like this in several other posts that I found were like this ... so its relevant, for ruby, using google-api-client (for any of google-apis), there are several inputs and outputs with authentication when using the api key, not OAuth ...

I set out this process (using the server side of the api server) in a space of code .

You must explicitly specify the authorzation nil parameter when building the client, otherwise the pearl will try to use OAuth for authentication, so when you call from the server using only the api key, you will always have 401 Unauthorized.

abode code - google-api-client for ruby

0
source

Source: https://habr.com/ru/post/897724/


All Articles