【问题标题】:Can not retrieve thumbnailLink from google drive API无法从 Google Drive API 检索 thumbnailLink
【发布时间】:2020-10-07 21:18:41
【问题描述】:

我正在尝试使用 python 和 google Drive API 从共享驱动器中的文件中获取 thumbnailLink,但是,文件信息不包括 thumbnailLink(尽管对于大多数文件,hasThumbnail 字段,我确实将其作为字段对于文件,值为 true)

我环顾四周,发现的解决方案似乎都不起作用(尽管这是我的第一个 python 项目以及我的第一个 google drive api 项目,所以我可能只是不知道我在做什么)

我尝试过的: - 将范围设置为“https://www.googleapis.com/auth/drive”(之前是 ..drive.metadata.readonly) - 使用通配符:results = drive.files().list(pageSize=10, fields="*",blablabla...)。例如,如果我尝试 fields="thumbnailLink" 它找不到任何文件。 - 获得列表后,我尝试使用该列表中每个文件的 ID 来执行 file = service.files().get(fileId=item_id, supportsAllDrives=True, fields="*").execute() 但同样的情况发生了,我有许多字段,包括设置为 true 的 hasThumbnail 字段,但没有缩略图链接。 - 我尝试使用官方网站上的“Try this API”控制台,实际上我确实获得了thumbnailLink! (具有与上述相同的参数)。所以我不明白为什么在我的应用程序请求时缺少它。

编辑(代码): 我有这样一种方法

SCOPES = ['https://www.googleapis.com/auth/drive']

def getDrive():
    """Shows basic usage of the Drive v3 API.
    Prints the names and ids of the first 10 files the user has access to.
    """
    creds = None
    # The file token.pickle stores the user's access and refresh tokens, and is
    # created automatically when the authorization flow completes for the first
    # time.
    if os.path.exists('token.pickle'):
        with open('token.pickle', 'rb') as token:
            creds = pickle.load(token)
    # If there are no (valid) credentials available, let the user log in.
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                'credentials.json', SCOPES)
            creds = flow.run_local_server(port=53209)
        # Save the credentials for the next run
        with open('token.pickle', 'wb') as token:
            pickle.dump(creds, token)

    service = build('drive', 'v3', credentials=creds)
    return service

然后我从这里调用它并获取文件:

def getFiles(request):
    drive = getDrive()
    # Call the Drive v3 API
    results = drive.files().list(
        pageSize=10, fields="*", driveId="blabla", includeItemsFromAllDrives=True, corpora="drive", supportsAllDrives=True).execute()

    items = results.get('files', [])
    getItems = []
    for item in items:
        item_id = item['id']
        getItems.append(drive.files().get(fileId=item_id, supportsAllDrives=True, fields="*").execute())
    if not items:
        print('No files found.')
    else:
        print('Files:')
        print(getItems)
        for item in items:
            # print(u'{0} ({1})'.format(item['name'], item['id']))
            print(item)

    return render(request, "index.html", {'files': getItems})

另外,是的,我确实使用服务帐户,我可以检索我需要的所有文件,而不是缩略图链接。

我认为调用 list() 然后调用 get() 是没有意义的,但我读到可以通过 get() 方法解决问题,但在我的情况下它不起作用。

【问题讨论】:

  • 我能问你的问题吗? 1. 请问您要检索缩略图链接的文件的mimeType 吗? 2. 我可以查看您当前的脚本来复制您的问题吗?
  • 你能提供你的完整代码吗?您使用的是服务帐号吗?
  • 您好,感谢您的快速回复!我已经更新了上面的问题,如果您还有其他问题,请告诉我

标签: python google-drive-api thumbnails


【解决方案1】:

问题在于响应的结构

如果您指定fields="*",则响应将类似于

{
 "kind": "drive#fileList",
  ...
 "files": [
  {
   "kind": "drive#file",
    ...
   "hasThumbnail": true,
   "thumbnailLink": "XXX",
   "thumbnailVersion": "XXX"
    ...
  }
  ..
 ]
}

所以,thumbnailLink 嵌套在 files 内。

为了检索它,请指定:

fields='files(id, thumbnailLink)'

【讨论】:

  • 谢谢,但这并不能解决问题。我已经尝试过你的实现,但是我只得到了返回的 id,没有 thumbnailLink。除此之外,如果我将“items”对象而不是“getItems”对象传递给上下文,我已经在items = results.get('files', []) 行中检索了“文件”部分你放在这里的模型json几乎就是我得到的,只是 thumbnailLink 字段完全丢失(即使对于那些具有“hasThumbnail:true”的项目
  • 所以当你指定fields="*"你得到"hasThumbnail": true但同时thumbNailLink是空的?只有当您使用服务帐户检索文件 n Python 时才会发生这种情况,而不是当您以您身份进行身份验证或使用 Try this API?
  • 完全正确。当我指定fields="*" 时,我得到"hasThumbnail"=true,甚至是缩略图版本,但没有thumbnailLinkTry this API 具有完全相同的复制粘贴参数确实返回了链接,而当我的应用程序接收到数据时,没有链接。
  • 听起来像是服务帐户的权限问题。您是否在没有服务帐户或模拟的情况下以编程方式尝试?
  • 我所做的是使用可以访问文件的现有用户帐户并为该帐户创建 API 密钥和所有内容。那有意义吗? (不太确定我是否理解这个问题,我认为我只是对此不熟悉)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-22
  • 1970-01-01
  • 2012-06-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多