Create a Public Google Doc via Google Drive API in Scala


In our earlier blog, we told about  “How to Create, Upload & Delete a Google Doc using Scala“. But the Google Docs that were created, had private access (by default), i.e., only the creator of Google Doc can access it.

So, now we continue to add one more feature to it i.e., to Create a Public Google Doc using Google Drive API in Scala. With this feature we can make our Google Docs public so, that anyone can read them but not modify them.

To add this feature, follow these steps:

1. Import following package to create a variable of type Permission().

import com.google.api.services.drive.model.Permission

 

2. Get the access token to access your drive

val access_token = "Your Drive's Access Token"

 

3. Set the mimeType (type of Google Doc) of your Google Doc

val mimeType = "mimeType"

MimeType can be Document/Spreadsheet/Presentation.

 

4. At last create the public Google Doc using following code

val service = prepareGoogleDrive(access_token)
val body = new File
    body.setMimeType(mimeType)
    body.setTitle("Untitled Google Doc")
val file = service.files.insert(body).execute
val permission = new Permission()
    permission.setRole("reader")
    permission.setType("anyone")
    permission.setValue("me")
service.permissions().insert(file.getId(), permission).execute()

For more information on different access levels of Google Docs, click here.

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

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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