Bùi Văn Thủ
Bùi Văn Thủ

Reputation: 393

Google Drive API permissions, how to disable findable by anyone on Internet?

I search for google but get no result, my code is as follow:

import sys
reload(sys)
sys.setdefaultencoding("utf-8")
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive

# this function used inside function do_the_work(drive)
def upload_gd(media_file, drive):
    print 'Try uploading ' + media_file

    xfile = drive.CreateFile()
    xfile.SetContentFile(media_file)
    xfile.Upload()
    print('Created file %s with mimeType %s' % (xfile['title'], xfile['mimeType']))

    permission = xfile.InsertPermission({
        'type': 'anyone',
        'value': 'anyone',
        'role': 'reader'})

    print 'Sharable link (to view) is:' + xfile['alternateLink']
    print 'Get direct link'
    file_id = xfile['alternateLink'].split('/')[-2]
    print 'file ID: ' + file_id
    d_link = 'https://drive.google.com/uc?export=download&id=' + file_id
    print 'Direct link is: ' + d_link

    return d_link

gauth = GoogleAuth()
gauth.LocalWebserverAuth()

drive = GoogleDrive(gauth)
do_the_work(drive)

And, the permissions I get for the file is:

Anyone can find and view

But, I only want anyone can view, but not findable as:

anyone can view

Upvotes: 1

Views: 857

Answers (2)

Robin Nabel
Robin Nabel

Reputation: 2310

You need to add the withLink field:

permission = xfile.InsertPermission({'type': 'anyone',
                                     'value': 'anyone',
                                     'role': 'reader',
                                     'withLink': True})  # <-- This field.

For all possible settings have a look at the API reference: link (PyDrive currently uses API v2)

And as an aside, you can get the ID of xfile with xfile['id'] so you don't need to split the alternate link.

All fields listed here can be accessed using xfile['<property name>'] after you call xfile.FetchMetadata(fetch_all=True). Using this you can extract the different types of file links from the file object, which will be more robust then your current method of achieving this.

Upvotes: 3

Linda Lawton - DaImTo
Linda Lawton - DaImTo

Reputation: 117146

permission = xfile.InsertPermission({'type': 'anyone',
                                     'value': 'anyone',
                                     'role': 'reader'})

With that statement you are essentially setting the file to public read only. Public read only is readable by everyone on the internet.

Answer: If you don't want to anyone on the internet to be able to find or view it then set type to group, domain, or user or better yet set value to the email address of the person you whish to be able to view the file.

Upvotes: 0

Related Questions