Skip to content

Instantly share code, notes, and snippets.

@sudhirj
Last active December 17, 2015 12:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sudhirj/5611509 to your computer and use it in GitHub Desktop.
Save sudhirj/5611509 to your computer and use it in GitHub Desktop.
The google ruby client docs on Github neglect to provide examples for the service account flow. This is saved off the old Google Code page.

###Authorization Most interactions with Google APIs require users to authorize applications via OAuth 2.0. The client library uses Signet to handle most aspects of authorization. For additional details about Google's OAuth support, see Google Developers.

Credentials can be managed at the connection level, as shown, or supplied on a per-request basis when calling execute. For server-to-server interactions, like those between a web application and Google Cloud Storage, Prediction, or BigQuery APIs, use service accounts. Assertions for service accounts are made with Google::APIClient::JWTAsserter.

client = Google::APIClient.new
key = Google::APIClient::PKCS12.load_key('client.p12', 'notasecret')
service_account = Google::APIClient::JWTAsserter.new(
    '123456-abcdef@developer.gserviceaccount.com',
    'https://www.googleapis.com/auth/prediction',
    key)
client.authorization = service_account.authorize
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment