Google Data on Rails

Tram Ho


Come to see you again, today I would like to introduce to you a way to connect directly to Google and call its open API easily.

As you have experience. In the process of developing an application, sometimes with specific requirements, we have to work with the develop apps of large systems like Google with applications like sending email … and often with newcomers. Beginners will have some difficulty configuring this place – so that the application can send and receive requests with other platforms.

So don’t let you wait any longer, we start right away: Google Data on Rails

PS. I used Ruby, the Rails framework for the tutorial


We need the configuration required: Ruby, RubyGems, Rails before we want to use gdata for our application. A small note here is that on Google’s documentation page, versions of Ruby and Rails are very old compared to the present time. So everyone when reading the document can be flexible in this place. Use some new version for newer.

After having a good environment, inside the developing application, we run:

After running this national command, if anyone is careful, you can run the following command:

to check whether the gem has been installed okie or not

Installation is complete, you can check the version of things to see the remainder. Now I go to the Authenticate section


Simple visualization like this: Currently I want to get api from an existing application. Therefore, from the perspective of the application, it is clear that they are late to verify and grant access to the requests from outside. So we need to log in to be able to access the requests.

Well, I see in this place the Google developer has a note like this: If I am developing desktop applications of all kinds (it’s been a long time, I used to learn java swing.  you can login using CliendLogin. As for Web applications, AuthSub or OAuth should be preferred.

We in turn go through each type of login to see how it looks!


Google it has a lot of platforms that provide API for customers to use. Typical examples like Gmail, Youtube … We will in turn login to each platform there.

Pretend we have a Google account that has:

Email: [email protected]

Password: “pa $$ word”

Login DocList

Login Youtube

If you are late to login to use Google’s full list service. You can login directly to its Base class. But in this case G Suite should be used

There is another place to note here. It is difficult for ClientLogin to pass through the logines that need CAPTCHA. What I need to do is add the CAPTCHA condition to these cases by calling the clientlogin() method clientlogin()

In the Google documentation page, it adds a pretty good link for you to learn more, I put it HERE !


To use AuthSub, first you need to generate AuthSubRequest URL first

It will generate the below link in authsub_link :

You can also add authsub_url to the client object. Each service class has been set a default authsub_scope as shown below

Corresponding URL:

Use session instead of authenticate ones for single-request.

The problem set out here is also simple. Instead of with each request to api. You need a token add on request to do authent. And the add token work is done again and again. Then we can create a session with a single access authent. Later in the session log, we can access the request comfortably.

As the above code clearly states. After receiving token-single request parrams. We add that token for the token of a session and authenticate on that session. If the token exists, the request pass is authenticate.

Once done, create the session. We need to put the values ​​to be secured in environment variables. On the original VD of the document being written like this:

Token management

AuthSub provides us with 2 additional handlers to manage tokens: AuthSubTokenInfor and AuthSubRevokeToken. The function of these two monkeys is to check the information, the validity of the token and withdraw.

You can look at the 2 examples below to see

Accessing feeds

Now we look at the methods to send requests to the api with specific examples.

GET (fetching data)

POST (creating new data)

This example is using the POST method to create a new data on the server. Specifically, it adds new [email protected] to docs with id is doc_id

PUT (updating data)

Similar to POST. You can use PUT to update the new data you need


Finally, Delete

Above I just introduced to you a pretty Lib that supports the connection and application of Google api from your developing application. I hope it will be of help to you when needed


Share the news now

Source : Viblo