【问题标题】:Unable to get thumbnail using Google Drive API无法使用 Google Drive API 获取缩略图
【发布时间】:2014-05-08 17:39:56
【问题描述】:

我的应用程序正在使用 OAuth2 服务帐户从用户的 Google 云端硬盘复制文件。我正在通过 Java 使用 Google Drive 客户端 api 来获取请求范围为“https://www.googleapis.com/auth/drive”的 Drive 对象。我可以制作 Google Docs 文档的副本,但无法检索 thumbnailLink。我收到 403 禁止错误。我很有信心这是谷歌方面的一个错误。如果我在获得 403 Forbidden 结果的行中的代码中放置一个断点,我可以(当以我要复制的 Google Drive 的用户身份登录时)使用 thumbnailLink 在我的浏览器中获取缩略图。

这是我正在使用的代码的重写 sn-p,其中 sourceFile 是从中复制的 com.google.api.services.drive.model.File,sourceDrive 是 com.google.api.services我上面提到的 .drive.Drive 对象:

File newFile = new File();
newFile.setTitle( sourceFile.getTitle() );
newFile.setDescription( sourceFile.getDescription() );
newFile.setParents( sourceFile.getParents() );
File copiedFile = sourceDrive.files().copy( sourceFile.getId(), newFile ).execute();
String thumbnailLink = copiedFile.getThumbnailLink();
HttpRequest request = sourceDrive.getRequestFactory().buildGetRequest( new GenericUrl( thumbnailLink ) );
HttpResponse response = request.execute();

如上所述,由于返回 403 Forbidden 错误,request.execute() 行会产生异常。如果我在上面代码 sn-p 的最后一行设置断点,我可以获取 thumbnailLink 并将其粘贴到我的浏览器中,该浏览器以从中复制驱动器的用户身份登录,它会成功返回缩略图.

【问题讨论】:

  • 我的 OAuth2 服务帐户(模拟从中复制文件的用户)得到与未经身份验证的用户在浏览器中得到的相同的错误:403。这是一个错误。您的客户端无权从此服务器获取 URL /kBawseILbP1SYEeCa_O-ir_NSgCWz93jyojJvPPlNGAtMSv93r77_2Q4gzhEZ_pZmJXZ2awVZDB882L5YLMqcpMgTaZj=s220。 (客户端 IP 地址:xx.xx.xx.xx)未经授权的用户这就是我们所知道的。
  • 我做了进一步的测试,将 sourceFile 复制到 newFile 对缩略图没有任何影响。即使在复制之前,我也无法从本机 Google Docs 文件中获取缩略图。
  • 我的问题听起来很像链接this post。此外,我认为这对我来说一直有效(正如我在帖子中提到的 mtheriault 所提到的那样)就在几周前。如果 Google 有人可以对此问题发表评论,那就太好了,因为它肯定是 Google 的错误。
  • 我遇到了同样的错误,只有原生文件也是如此。自 2.5 年前以来,代码一直未受影响,因此这是 Google 对其服务器的某种更改。

标签: google-drive-api google-oauth google-apps-marketplace


【解决方案1】:

对于您可以使用

创建的缩略图链接

https://drive.google.com/thumbnail?id={YOUR_IMAGE_FILE_ID}

默认返回220px的缩略图(max-width-220px max-height-220px,保持纵横比)

您可以通过链接发送更多参数,例如宽度、高度 然后我们需要使用'sz'查询字符串 https://drive.google.com/thumbnail?sz=w100-h100&id={YOUR_IMAGE_FILE_ID}

这里的 sz=w100-h100 表示高度 100px 和宽度 100px。你也可以通过他们中的任何一个。 如果您同时发送高度和宽度,则必须确定这些值是否保持图像的原始纵横比。

【讨论】:

  • 你今天为我节省了很多时间。谢谢。
【解决方案2】:

我已经在related question 中发布了这个答案,但它也应该适用于这里。我们最近进行了更改以解决此问题。请再次尝试您的代码,看看您是否仍然收到此错误。我能够运行以下代码以成功下载我的 Google 文档的缩略图。

# Get oauth credentials
...
# Authorize an http object
http = httplib2.Http()
http = credentials.authorize(http)

drive_service = build('drive', 'v2', http=http)

# Google Document type ID
docId = '1ns9x5BMIZAeUR-eXerqgpaHBBGkl_-_KCVpVoV5opn8'
files = drive_service.files().get(fileId=docId).execute()

thumbnailLink = files['thumbnailLink']
print 'Downloading thumbnail at: ', thumbnailLink
response, content = http.request(thumbnailLink)
print response.status

with open('thumbnail.jpg', 'wb') as f:
  f.write(content)

【讨论】:

    【解决方案3】:

    更新:更新请求字段的讨论。

    据我所知,实际上没有关于此解决方案的好的文档。但是,在同事和 REST API 操作的帮助下,我得到了解决方案。

    要使用 Drive Java 客户端库获取有效的 thumbnailLink 值,您必须修改查询。由于您正在执行自定义字段请求,因此您必须确保添加您想要的其他字段。这是我如何让它工作的:

    Drive.Files.List request = yourGoogleDriveService.files()
                        .list()
                        .setFields("files/thumbnailLink, files/name, files/mimeType, files/id")
                        .setQ("Your file param and/or mime query");
    
    FileList files = request.execute();
    files.getFiles();  //Each File in the collection will have a valid thumbnailLink
    

    希望这会有所帮助!

    【讨论】:

      【解决方案4】:

      我也必须这样做。这对我有用:

      final File file = drive.files().get(fileId).execute();
      final String thumbnailLink = file.getThumbnailLink();
      
      HttpRequest request = drive.getRequestFactory().buildGetRequest( new GenericUrl( thumbnailLink ) );
      HttpResponse response = request.execute();
      ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
      response.download(byteArrayOutputStream);
      InputStream is = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
      

      【讨论】:

        【解决方案5】:

        如果您使用的是服务帐户,则需要通过电子邮件传递委托凭据,否则将返回 404 错误。

        SCOPES = ['https://www.googleapis.com/auth/drive.readonly']
        
        def get_thumbnail(email, thumbnailLink, fileName):
            with open('creds.json', 'r') as f:
                key = json.loads(f.read())
            f.close()
            print("fileName: ", fileName)
        
            creds = ServiceAccountCredentials.from_json_keyfile_dict(
                key,
                scopes=SCOPES
            )
            creds_del = creds.create_delegated(email)
            http = httplib2.Http()
            http = creds_del.authorize(http)
            print('Downloading thumbnail at: ', thumbnailLink)
            response, content = http.request(thumbnailLink)
        
            with open(name + '.jpg', 'wb') as f:
                f.write(content)
            f.close()
        

        【讨论】:

          猜你喜欢
          • 2017-12-14
          • 2017-05-12
          • 2016-12-10
          • 2020-07-07
          • 2017-08-15
          • 1970-01-01
          • 2023-03-07
          • 2021-08-06
          • 1970-01-01
          相关资源
          最近更新 更多