Create, Upload & Delete Google Docs using Play Scala application

This blog talks about creating, uploading & deleting Google Docs using a Play Scala application. We have used oauth2 in order to communicate with the Google Drive to perform various Google Doc operations. Let us see the process step by step.

1.First integrate Google Drive infrastructure in your Play Scala Application. You can read about it from here to know how to do it.

2. Then in order to use Google Drive, first you have to prepare it. Now, to prepare it you have add this code.

val CLIENT_ID = "your_app's_client_id"
 val CLIENT_SECRET = "your_app's_client_secret"
 val httpTransport = new NetHttpTransport
 val jsonFactory = new JacksonFactory

 // Preparing Google Drive
 def prepareGoogleDrive(accessToken: String): Drive = {

 //Build the Google credentials and make the Drive ready to interact
 val credential = new GoogleCredential.Builder()
 .setClientSecrets(CLIENT_ID, CLIENT_SECRET)
 //Create a new authorized API client
 new Drive.Builder(httpTransport, jsonFactory, credential).build()

Here you have to pass the Access Token for accessing the Google Drive.

3. Then to Create a new Google Doc add this code.

def createANewGoogleDocument(code: String, mimeType: String): List[String] = {
 val service = prepareGoogleDrive(code)
 val body = new File
 val docType = mimeType.substring(mimeType.lastIndexOf(".") + 1)
 body.setTitle("Untitled " + docType)
 val file = service.files.insert(body).execute
 List(file.getAlternateLink, file.getTitle(), file.getThumbnailLink())

Here you have to pass the Access Token (code) and the type (mimeType) of Google Doc which you want to create. The type of Google Doc can be Document/Spreadsheet/Presentation.

4. To Upload a file from your Hard Disk to Google Drive add this code.

def uploadToGoogleDrive(accessToken: String, fileToUpload:, fileName: String, contentType: String): String = {
 val service = prepareGoogleDrive(accessToken)

 //Insert a file
 val body = new File
 val fileContent: = fileToUpload
 val mediaContent = new FileContent(contentType, fileContent)
 //Inserting the files
 val file = service.files.insert(body, mediaContent).execute()


Here you have to pass Access Token, file to upload, file name and content type which you want to upload to Google Drive

5. At last to Delete a Google Doc add this code.

def deleteAGoogleDocument(code: String, docId: String) = {
 val service = prepareGoogleDrive(code)
 try {
 } catch {
 case ex: Exception => println(ex)

In here you to pass Access token and Doc Id in order to delete it from Google Drive. Document Id is present in Document Url. For Example, if your Google Doc URL is this _4uX6VZ9HVncaIRd-YXBZZ0FS0W-1U7UJ08/ then the document id is 1l2A2iNm5_4uX6VZ9HVncaIRd-YXBZZ0FS0W-1U7UJ08.

Note :- In case of Google Spreadsheet Doc Id is key. For example if the spreadsheet’s URL is this https: // then doc Id is 0AiZ5C9Yq7QMUdG84X3V1M1pNMlRvM0xnNldVSzRSa3c. So, while deleting a spreadsheet from Google Docs pass this key as Doc Id to the function given above.

This entry was posted in Scala, Web and tagged , , , , . Bookmark the permalink.

One Response to Create, Upload & Delete Google Docs using Play Scala application

  1. Pingback: Create a Public Google Doc via Google Drive API in Scala | Knoldus

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s