使用Google云端硬盘获取WebViewLinks

超级重男

我刚刚开始尝试使用Google Drive API。使用快速入门指南设置身份验证,可以打印文件列表,甚至可以制作副本。一切正常,但是尝试从云端硬盘上的文件访问数据时遇到麻烦。特别是,我试图获取一个WebViewLink,但是当我打电话时,.get我只收到一个几乎没有文件元数据的小词典。该文档使所有数据看起来默认都应该存在,但是没有出现。我找不到任何方法来标记请求任何其他信息。

credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
service = discovery.build('drive', 'v3', http=http)

results = service.files().list(fields="nextPageToken, files(id, name)").execute()
items = results.get('files', [])
if not items:
    print('No files found.')
else:
    print('Files:')
    for item in items:
        print(item['name'], item['id'])
        if "Target File" in item['name']:
            d = service.files().get(fileId=item['id']).execute()
            print(repr(d))

这是上面代码的输出:(格式化是我的工作)

{u'mimeType': u'application/vnd.google-apps.document', 
 u'kind': u'drive#file',
 u'id': u'1VO9cC8mGM67onVYx3_2f-SYzLJPR4_LteQzILdWJgDE',
 u'name': u'Fix TVP Licence Issues'}

对于任何对代码感到困惑的人,都缺少一些东西,这些东西仅仅get_credentials是API快速入门页面中的基本功能以及一些常量和导入。为了完整起见,以下是我的代码中未经修改的所有内容:

from __future__ import print_function
import httplib2
import os

from apiclient import discovery
import oauth2client
from oauth2client import client
from oauth2client import tools

SCOPES = 'https://www.googleapis.com/auth/drive'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'Drive API Python Quickstart'

try:
    import argparse
    flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
    flags = None


def get_credentials():
    """Gets valid user credentials from storage.

    If nothing has been stored, or if the stored credentials are invalid,
    the OAuth2 flow is completed to obtain the new credentials.

    Returns:
        Credentials, the obtained credential.
    """
    home_dir = os.path.expanduser('~')
    credential_dir = os.path.join(home_dir, '.credentials')
    if not os.path.exists(credential_dir):
        os.makedirs(credential_dir)
    credential_path = os.path.join(credential_dir,
                                   'drive-python-quickstart.json')

    store = oauth2client.file.Storage(credential_path)
    credentials = store.get()
    if not credentials or credentials.invalid:
        flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
        flow.user_agent = APPLICATION_NAME
        if flags:
            credentials = tools.run_flow(flow, store, flags)
        else: # Needed only for compatibility with Python 2.6
            credentials = tools.run(flow, store)
        print('Storing credentials to ' + credential_path)
    return credentials

那么,缺少的是什么,我如何才能获得API以返回所有现在还没有出现的所有额外元数据呢?

安德烈斯

你很亲密 使用更高版本的Drive API v3,要检索其他元数据属性,您将必须添加fields参数以指定要包含在部分响应中的其他属性。

在您的情况下,由于您要检索WebViewLink属性,因此您的请求应类似于以下内容:

results = service.files().list(
        pageSize=10,fields="nextPageToken, files(id, name, webViewLink)").execute() 

要显示响应中的项目:

for item in items:
            print('{0} {1} {2}'.format(item['name'], item['id'], item['webViewLink']))

我还建议您尝试使用API Explorer进行尝试,以便您可以查看想要在响应中显示的其他元数据属性。

祝你好运,希望这能帮到你 !:)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Google云端硬盘使用什么技术来获取实时更新?

来自分类Dev

在2020年6月1日之后使用Google云端硬盘API获取“我的云端硬盘”中的文件列表?

来自分类Dev

不再使用查询获取Google云端硬盘应用数据文件夹的内容

来自分类Dev

我正在尝试使用Google云端硬盘API获取文件的直接下载链接

来自分类Dev

无法使用Google云端硬盘API获取文件所有者

来自分类Dev

Google云端硬盘CORS

来自分类Dev

集成Google云端硬盘

来自分类Dev

获取指向Google云端硬盘演示文稿文件的链接

来自分类Dev

Android-Google云端硬盘-无法从查询中获取结果

来自分类Dev

通过共享链接从Google云端硬盘获取文件

来自分类Dev

如何从Google云端硬盘获取图像数据集到Colab?

来自分类Dev

Google云端硬盘从文件夹获取文件

来自分类Dev

Google云端硬盘API通过API密钥获取AppProperty

来自分类Dev

如何在Google云端硬盘中获取图片文件的网址

来自分类Dev

获取Google云端硬盘文件夹的ResourceID

来自分类Dev

是否可以获取Google云端硬盘文件的永久链接?

来自分类Dev

使用Java从Google云端硬盘下载Google文档文件

来自分类Dev

使用Google API的Google云端硬盘上传文件

来自分类Dev

使用Google Docs API从Google云端硬盘插入图像

来自分类Dev

使用Google Picker从Google云端硬盘下载文件

来自分类Dev

使用http上传到Google云端硬盘文件夹

来自分类Dev

使用S3或gcs等Google云端硬盘

来自分类Dev

使用Powershell从Google云端硬盘下载文件

来自分类Dev

使用当前活动进行Google云端硬盘集成

来自分类Dev

使用意图将文件上传到Google云端硬盘

来自分类Dev

使用Google云端硬盘托管网站时遇到的问题

来自分类Dev

使用PyDrive将图片上传到Google云端硬盘

来自分类Dev

使用REST API从Google云端硬盘下载图像

来自分类Dev

使用Python从Google云端硬盘下载文件

Related 相关文章

  1. 1

    Google云端硬盘使用什么技术来获取实时更新?

  2. 2

    在2020年6月1日之后使用Google云端硬盘API获取“我的云端硬盘”中的文件列表?

  3. 3

    不再使用查询获取Google云端硬盘应用数据文件夹的内容

  4. 4

    我正在尝试使用Google云端硬盘API获取文件的直接下载链接

  5. 5

    无法使用Google云端硬盘API获取文件所有者

  6. 6

    Google云端硬盘CORS

  7. 7

    集成Google云端硬盘

  8. 8

    获取指向Google云端硬盘演示文稿文件的链接

  9. 9

    Android-Google云端硬盘-无法从查询中获取结果

  10. 10

    通过共享链接从Google云端硬盘获取文件

  11. 11

    如何从Google云端硬盘获取图像数据集到Colab?

  12. 12

    Google云端硬盘从文件夹获取文件

  13. 13

    Google云端硬盘API通过API密钥获取AppProperty

  14. 14

    如何在Google云端硬盘中获取图片文件的网址

  15. 15

    获取Google云端硬盘文件夹的ResourceID

  16. 16

    是否可以获取Google云端硬盘文件的永久链接?

  17. 17

    使用Java从Google云端硬盘下载Google文档文件

  18. 18

    使用Google API的Google云端硬盘上传文件

  19. 19

    使用Google Docs API从Google云端硬盘插入图像

  20. 20

    使用Google Picker从Google云端硬盘下载文件

  21. 21

    使用http上传到Google云端硬盘文件夹

  22. 22

    使用S3或gcs等Google云端硬盘

  23. 23

    使用Powershell从Google云端硬盘下载文件

  24. 24

    使用当前活动进行Google云端硬盘集成

  25. 25

    使用意图将文件上传到Google云端硬盘

  26. 26

    使用Google云端硬盘托管网站时遇到的问题

  27. 27

    使用PyDrive将图片上传到Google云端硬盘

  28. 28

    使用REST API从Google云端硬盘下载图像

  29. 29

    使用Python从Google云端硬盘下载文件

热门标签

归档