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

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

问题描述

我的应用程序正在使用 OAuth2 服务帐户从用户的 Google 云端硬盘复制文件.我正在通过 Java 使用 Google Drive 客户端 api 来获取请求范围为 "https:/的 Drive 对象/www.googleapis.com/auth/drive".我可以制作 Google Docs 文档的副本,但无法检索 thumbnailLink.我收到 403 禁止错误.我很有信心这是谷歌方面的一个错误.如果我在获得 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 并将其粘贴到我的浏览器中,该浏览器以要从中复制驱动器的用户身份登录,并且它会成功返回缩略图.

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.

推荐答案

您可以使用创建的缩略图链接

for the thumbnail link you can create with

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

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

By default that will return thumbnail of 220 px(max-width-220px max-height-220px, keeping the aspect ratio)

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

there are more parameters you can send with the link such as width, height Then we need to use 'sz' querystring https://drive.google.com/thumbnail?sz=w100-h100&id={YOUR_IMAGE_FILE_ID}

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

here sz=w100-h100 means height 100px and width 100px. you can pass anyone of them too. If you are sending both height and width then have to be sure about those values whether it is maintaining the original aspect ratio of the image.

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

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