Get a Google OAuth2 Access Token via Ruby in minutes

- Jonathan Otto email me: jonathan.otto@gmail.com

Consternation

Google's API documentation is verbose, and the error messages you receive during this process are not helpful. Here's a minimalist, "get it done" approach to connecting to Google's OAuth2 API using Ruby.

Try Google's OAuth2 API Playground

Use the OAuth2 playground to test the APIs immediately and observe the HTTP requests and responses. This "playground" what I used to build the script below. It was easier to reverse engineer it than it was the read the documentation.

Or... follow these steps and try my simple Ruby command line script to get an OAuth2 access token and code for making API requests

get the OAuth2 gem and some API credentials first.

  1. gem install oauth2
  2. go to https://code.google.com/apis/console/
  3. pick the services you want
  4. click "API access" link in the sidebar
  5. click "Create an Oauth 2.0 client ID"
  6. type some arbitrary branding info
  7. application type is web application
  8. set site or hostname to localhost
  9. click create
  10. copy and paste (click "view raw" at the bottom) the Gist below onto your computer, save it, and copy and paste the "Client ID" and "Client secret" from the API console into the variables in the Gist below
  11. find a scope, for Google Affiliate Network, it's https://www.googleapis.com/auth/gan and paste it into scope variable in the Gist below
    • the "API Reference" for any one API method will list the required scope in the "Authorization" section
  12. ...and here's the Gist! Run it with Ruby from the command line after you fill in the variables at the top and it will tell you how to use the Access Token.