无法使用Google Drive API获取缩略图 [英] Unable to get thumbnail using Google Drive API

查看:158
本文介绍了无法使用Google Drive API获取缩略图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My application is using an OAuth2 service account to copy a file from a user's Google Drive. I'm using the Google Drive client api via Java to obtain a Drive object with the requested scope being "https://www.googleapis.com/auth/drive". I'm able to make a copy of the Google Docs document, but the thumbnailLink is not retrievable. I'm getting a 403 Forbidden error. I'm quite confident this is a bug on the Google side. If I put a breakpoint in my code on he line that gets the 403 Forbidden result, I can (when logged in as the user whose Google Drive I'm copying from) use the thumbnailLink to get the thumbnail in my browser.

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

Here's a rewritten snippet of the code I'm using, where sourceFile is the com.google.api.services.drive.model.File that is being copied from and sourceDrive is the com.google.api.services.drive.Drive object that I mentioned above:

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()行会产生异常回。如果我在上面的代码片段的最后一行放置了一个断点,我可以将thumbnailLink粘贴到我的浏览器中,该浏览器以其用户的驱动器被复制的用户身份登录,并且成功返回缩略图。 / p>

As mentioned above, the request.execute() line produces an exception due to the 403 Forbidden error being returned. If I put a breakpoint on the last line of the code snippet above, I am able to take the thumbnailLink and paste it into my browser that is logged in as the user whose Drive is being copied from and it gets the thumbnail returned successfully.

推荐答案

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

I've posted this answer in a related question but it should be applicable here as well. We've recently made a change to fix this issue. Please try your code again and see if you are still getting this error. I was able to run the following code to successfully download a thumbnail of my Google Document.

# 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)

这篇关于无法使用Google Drive API获取缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆