cwj*_*jos 10 google-apps-marketplace google-drive-api google-oauth
我的应用程序使用OAuth2服务帐户从用户的Google云端硬盘复制文件.我正在通过Java使用Google云端硬盘客户端API来获取所请求范围为" https://www.googleapis.com/auth/drive " 的云端硬盘对象.我可以复制Google Docs文档,但thumbnailLink不可检索.我收到403 Forbidden错误.我非常有信心这是Google方面的一个错误.如果我在我的代码中设置一个断点来获取403 Forbidden结果,我可以(当我正在复制Google Drive的用户登录时)使用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();
Run Code Online (Sandbox Code Playgroud)
如上所述,request.execute()行由于返回403 Forbidden错误而产生异常.如果我在上面的代码片段的最后一行放置一个断点,我可以将thumbnailLink粘贴到我的浏览器中,该浏览器以正在复制其驱动器的用户身份登录并成功返回缩略图.
小智 6
对于您可以创建的缩略图链接
https://drive.google.com/thumbnail?id={YOUR_IMAGE_FILE_ID}
默认情况下,将返回 220 px 的缩略图(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
我已经在相关问题中发布了这个答案,但它也应该适用于此处。我们最近进行了更改来解决此问题。请重试您的代码,看看是否仍然出现此错误。我能够运行以下代码来成功下载我的 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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5086 次 |
| 最近记录: |