【问题标题】:Google Drive API permissions, how to disable findable by anyone on Internet?Google Drive API 权限,如何禁用互联网上的任何人都可以找到?
【发布时间】:2017-03-09 03:08:07
【问题描述】:

我搜索谷歌但没有结果,我的代码如下:

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)

而且,我获得的文件权限是:



但是,我只希望任何人都可以查看,但无法找到:

【问题讨论】:

    标签: python google-api google-drive-api google-api-python-client pydrive


    【解决方案1】:

    您需要添加withLink字段:

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

    对于所有可能的设置,请查看 API 参考:link(PyDrive 当前使用 API v2)

    顺便说一句,您可以使用xfile['id'] 获得xfile 的ID,因此您无需拆分备用链接。

    在调用xfile.FetchMetadata(fetch_all=True) 后,可以使用xfile['&lt;property name&gt;'] 访问here 列出的所有字段。使用它,您可以从文件对象中提取不同类型的文件链接,这将比您当前的实现方法更强大。

    【讨论】:

    • 请注意,pydrive目前使用的是drive api v2
    • 是的,谢谢@BùiVănThủ 强调这一点。上面指定的链接链接到 v2 - 我编辑了帖子以明确这一点:)
    【解决方案2】:
    permission = xfile.InsertPermission({'type': 'anyone',
                                         'value': 'anyone',
                                         'role': 'reader'})
    

    使用该语句,您实际上是将文件设置为公共只读。公共只读是互联网上的每个人都可以阅读的。

    答案:如果您不希望 Internet 上的任何人能够找到或查看它,请将类型设置为 groupdomainuser 或更好将值设置为您希望能够查看文件的人的电子邮件地址。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-06
      • 2016-09-02
      相关资源
      最近更新 更多