Integrating Google Drive infrastructure in Play Scala application

Table of contents
Reading Time: 3 minutes

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 xxxxxxxxxxxx.apps.googleusercontent.com
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 https://accounts.google.com/o/oauth2/token 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 application5 min read

  1. You can use Scala Dispatch http://dispatch.databinder.net/Dispatch.html
    And transform this:

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

    Logger.info(code)

    con.setRequestMethod("POST");

    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";
    Logger.info(urlParameters)

    con.setDoOutput(true)
    val wr = new DataOutputStream(con.getOutputStream)
    wr.writeBytes(urlParameters)
    wr.flush
    wr.close
    val in = new BufferedReader(new InputStreamReader(con.getInputStream))
    val response = new StringBuffer

    while (in.readLine != null) {
    response.append(in.readLine)
    }
    in.close
    Logger.info(response.toString)

    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)
    tokenJson.map(Logger.info(_))

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

    val scopes = "https://www.googleapis.com/auth/youtube.readonly" :: Nil

    and use scopes.asJava

    Anyway, thanks for your post! 🙂

Comments are closed.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading