Get a Google OAuth2 Access Token via Ruby in minutes
- Jonathan Otto email me: email@example.com
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.
gem install oauth2
- go to https://code.google.com/apis/console/
- pick the services you want
- click "API access" link in the sidebar
- click "Create an Oauth 2.0 client ID"
- type some arbitrary branding info
- application type is web application
- set site or hostname to localhost
- click create
- 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
- 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
- ...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.