Google API Authorization with Ruby using long lasting Refresh Token

Google API Authorization with Ruby on Rails

Authorization is the most important thing when it comes to API integration.

There are multiple ways for authorization, based on whom your code access Google on behalf of:

  1. On Behalf of yourself (Command line authorization).
  2. On Behalf of the user who accesses your web app (Web-based authorization).
  3. On Behalf of no existing users (Service account).

Today we will see how to achieve Web-based authorization. There are two ways with the help of which we can implement this.

Checkout: Top 12 Website Examples Built with Ruby on Rails

How to do it?

With auth_url

With Long-lasting refresh token

Today We will be looking at, how authorization can be implemented with long lasting refresh token. So here we Go! We will use the Google API client Ruby to see how long lasting refresh token can be used.

Step-1: Create a Web app and obtain refresh token and other credentials.

This can be done with the Oauth2 Playground at https://developers.google.com/oauthplayground.

Then,

  1. Create the Google Account (eg. my.drive.app@gmail.com)
  2. Use the API console to register the mydriveapp (https://console.developers.google.com/apis/credentials/oauthclient?project=mydriveapp or just https://console.developers.google.com/apis/)
  3. Create a new set of credentials (NB OAuth Client ID not Service Account Key and then choose “Web Application” from the selection)
  4. Include https://developers.google.com/oauthplayground as a valid redirect URI
  5. Note the client ID (web app) and Client Secret
  6. Login as my.drive.app@gmail.com
  7. Go to Oauth2 playground
  8. In Settings (gear icon), set
    • Oauth flow: server
    • Access type: offline
    • Use your own OAuth credentials: TICK
    • Client Id and Client Secret: from step 5
  9. Click Step 1 and choose Drive API https://www.googleapis.com/auth/drive (having said that, this technique also works for any of the Google APIs listed).
  10. Click Authorize APIs. You will be prompted to choose your Google account and confirm access
  11. Click Step 2 and “Exchange Authorization code for tokens”.
  12. Copy the returned Refresh Token and paste it into your app, source code or into some form of storage from where your app can retrieve it.

Now we have our credentials Client Id, Client Secret and refresh token with us from the Google API client library for Ruby.

Lets write some ruby code to get it to work…

Here we will be using the googleauth library, so create a simple Google API client ruby file and include this library.

require "googleauth"

Now we have to create an instance of Google::Auth::UserRefreshCredentials so set credentials for our user,

and done! Now you can use this credential instance to authorize any of the Google service you wish to use.

You can use this as follows, here I am using Google Drive API so we need to add this first.

That’s it guys, and now we can use a service object to call any of the services of Google Drive API.

So our final code will look like:

That’s it! Happy coding!!!

Click here for more details…

Consulting is free – let us help you grow!

Related posts

Rollout Gem in Ruby on Rails: Deploy New Feature on Production in Controlled Way

by Leena Chaudhari
7 years ago

Why Choosing Laravel is a Smart Decision for Developing Healthcare Application?

by Parth Barot
4 years ago

Integrate Stripe Payment Gateway in Spree Commerce

by Sanjay Prajapati
9 years ago
Exit mobile version