Integrating Google Drive infrastructure in Play Scala application

This blog talks about integrating the Google Drive infrastructure in Play Scala application as like Assembla and some others. We used oauth2 in order to communicate with the Google using Access Token and Refresh Token. Let us see the process step by step.

1. Prepare the browser request URL in order to get the authorization code.

Parameters required by this URL :

ClientId : Your Google application’s client Id. Looks like
Redirect URI : The registered URI with the application.
Scopes : : Specific information type that you want to demand from user.
Access Type : Should be set as “offline”.
Response Type : Should be set as “code” as we are demanding the authorization code. This code will be exchanged for the access token and the one time refresh token.

2. When the users will hit this url , they’ll be asked to Log in and allowing the application to access of their Google Drive’s documents and the other basic information (Depends upon the scope that we’ve defined in url). Once the users have successfully authenticated their Google accounts , the authorization code will be sent to the Redirect URI specified in the browser’s request url as request’s query parameter.

3. Once you receive the authorization code over the redirect uri that you’ve specified as like above , use this code for getting the Access Token and one time Refresh Token from Google by making a POST request on url. Again you’ve to send the basic credentials like Client_Id , Client_Secret , Redirect_URI etc along with this request.

You’ll get the following data in response.

5. Extract out the access token and the refresh token from the response.

Refresh token will be obtained only first time when the user will allow the access to their information. Don’t forget to save it in to your database. Next time when you’ll generate access token using this refresh token.

6. Now we are having access token so its time to set the Google credentials in order to make the Drive ready.

7. Now our Drive is ready. We can now upload the documents or fetch all the documents of a user’s Google drive and more depending upon the scope defined. Below is how you’ve to upload a file to Google drive and viewing the already existing files of a user.

8. Next time when the user will Log in to the system , you’d having refresh token already of the Google account of that particular user. You can use this refresh token in order to generate the new access token for the communication with user’s Google drive. Here is how you can generate the new access token from the previously existing refresh token.

This is the pretty straightforward way of how you can communicate with the Google Drive. Thank you folks !

3 thoughts on “Integrating Google Drive infrastructure in Play Scala application

  1. You can use Scala Dispatch
    And transform this:

    val obj = new URL(tokenURI)
    val con = obj.openConnection().asInstanceOf[HttpsURLConnection]


    val urlParameters = s"code=${code}&client_id=${clientId}&client_secret=${clientSecret}&redirect_uri=${redirectURI}&grant_type=authorization_code&Content-Type=application/x-www-form-urlencoded";

    val wr = new DataOutputStream(con.getOutputStream)
    val in = new BufferedReader(new InputStreamReader(con.getInputStream))
    val response = new StringBuffer

    while (in.readLine != null) {

    Into this:

    val tokenRequest = url(tokenURI) < code, "client_id" -> clientId, "client_secret" -> clientSecret, "redirect_uri" -> redirectURI, "grant_type" -> "authorization_code", "Content-Type" -> "application/x-www-form-urlencoded")
    val tokenJson = Http(tokenRequest OK as.String)

    Also, in the first line, you can define the scopes like this:

    val scopes = "" :: Nil

    and use scopes.asJava

    Anyway, thanks for your post! 🙂

Leave a Reply

%d bloggers like this: