使用Google Drive API无法获取缩略图

11
我的应用正在使用OAuth2服务帐户从用户的Google Drive中复制文件。我使用Java通过Google Drive客户端api获取具有所请求范围的Drive对象,该范围为"https://www.googleapis.com/auth/drive"。我能够复制Google Docs文档,但无法检索thumbnailLink。我收到了403 Forbidden错误。我非常有信心这是Google方面的一个bug。如果我在获取403 Forbidden结果的代码行上设置断点,(当作为我要复制的用户登录时)我可以使用thumbnailLink在浏览器中获取缩略图。
以下是我正在使用的代码片段的重写版本,其中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();

如上所述,request.execute()这一行代码由于返回了403禁止错误而导致出现异常。如果我在上面代码片段的最后一行设置断点,我可以获取thumbnailLink并将其粘贴到以正在复制其Drive的用户登录的浏览器中,成功地返回缩略图。


另外需要注意的是,如果sourceFile是一个上传的文件(例如PDF或图像),我可以成功地检索到缩略图,但当它是本地的Google文档时则不行。 - cwjos
我的OAuth2服务帐户(代表正在复制文件的用户)与未经身份验证的用户在浏览器中获得相同的错误:403。这是一个错误。您的客户端没有权限获取此服务器上的URL / kBawseILbP1SYEeCa_O-ir_NSgCWz93jyojJvPPlNGAtMSv93r77_2Q4gzhEZ_pZmJXZ2awVZDB882L5YLMqcpMgTaZj = s220。 (客户端IP地址:xx.xx.xx.xx)未经授权的用户,这就是我们所知道的。 - cwjos
我进行了进一步的测试,将源文件复制到新文件并没有对缩略图产生任何影响。即使在复制之前,我也无法从本地的Google Docs文件中获取缩略图。 - cwjos
1
我的问题听起来非常像这篇帖子。而且,我认为这在几周前对我有效(正如我所引用的帖子中mtheriault所提到的)。如果Google的某个人能够就此问题发表评论,那将是很好的,因为它明显是一个Google的bug。 - cwjos
3
我遇到了相同的错误,甚至在使用原生文件时也是如此。这段代码自2.5年前以来一直没有改动,因此应该是Google服务器上某种变化导致的。 - ernestoalejo
5个回答

5

对于缩略图链接,您可以使用以下方式创建:

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

默认情况下,它将返回最大宽度为220像素(最大宽度和最大高度均为220像素,保持宽高比)的缩略图。

您可以通过链接发送更多参数,例如宽度、高度,然后我们需要使用“sz”查询字符串。

https://drive.google.com/thumbnail?sz=w100-h100&id={YOUR_IMAGE_FILE_ID}

这里的sz=w100-h100表示高度为100像素,宽度为100像素。您也可以传递其中任何一个值。 如果您同时发送高度和宽度,则必须确保这些值是否保持了原始图像的宽高比。


你今天为我节省了很多时间。谢谢。 - Daniel Rch.

2
我已经在相关问题中发布了这个答案,但它也适用于这里。我们最近进行了更改以解决此问题。请再次尝试您的代码,并查看是否仍然出现此错误。我能够运行以下代码成功下载我的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)

1

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

据我所知,关于此问题的解决方案没有好的文档。然而,在一位同事和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

希望这有所帮助!

0

我也曾经遇到过同样的问题。以下是对我有效的解决方法:

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());

0
如果您正在使用服务账户,则需要传递委派凭据,其中包含主题电子邮件,否则将返回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()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接