Google drive api 新的缩略图功能-如何在Java中使用

Google drive api 新的缩略图功能-如何在Java中使用,google-drive-api,Google Drive Api,我想使用最新的JavaSDK利用GoogleDrive的新缩略图功能。 但我得到的只是一个糟糕的请求响应。 有人可以发布此功能的工作示例吗? 我只是简单地设置了mimetype和Base64编码的图像数据,但这不起作用——至少对于file.insert和file.patch是这样 更新: 这是我的(简化的)代码片段。如果没有新的缩略图,它可以正常工作-编码的图像数据看起来也不错: ... Drive drive = getDrive(account); File file = new File(

我想使用最新的JavaSDK利用GoogleDrive的新缩略图功能。 但我得到的只是一个糟糕的请求响应。 有人可以发布此功能的工作示例吗? 我只是简单地设置了mimetype和Base64编码的图像数据,但这不起作用——至少对于file.insert和file.patch是这样

更新: 这是我的(简化的)代码片段。如果没有新的缩略图,它可以正常工作-编码的图像数据看起来也不错:

...
Drive drive = getDrive(account);
File file = new File();
file.setTitle(name);
...
HTTPResponse response = urlFetch.fetch(new URL(imageUrl));
String encodedImage = new String(Base64.encodeBase64(response.getContent()));
Thumbnail thumbnail = new Thumbnail();
thumbnail.setImage(encodedImage);
thumbnail.setMimeType("image/png");
file.setThumbnail(thumbnail);
file = drive.files().insert(file).execute();
以下是回应:

com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 OK
{
  "code" : 400,
  "errors" : [ {
    "domain" : "global",
    "message" : "Bad Request",
    "reason" : "badRequest"
  } ],
  "message" : "Bad Request"
}
    at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:143)
    at com.google.api.client.googleapis.json.GoogleJsonResponseException.execute(GoogleJsonResponseException.java:187)
    at com.google.api.client.googleapis.services.GoogleClient.executeUnparsed(GoogleClient.java:279)
    at com.google.api.client.http.json.JsonHttpRequest.executeUnparsed(JsonHttpRequest.java:207)

缩略图图像需要编码为Base64 UrlSafe字符串。 使用


对图像进行编码修复了该问题。

400错误请求响应应包含一条描述性错误消息,说明请求无效的原因。您能显示您的代码和完整的响应吗?您能输出Base 64编码的字符串以便我们可以试用吗?顺便问一下,您能使用Base64.encodeBase64String()而不是新字符串(Base64.encodeBase64(…您能打印Base64.encodeBase64String()返回的字符串吗?因此,我们可以检查它是否正确,在我们这方面尝试一下,如果有错误,请将其作为我们团队中的一个示例发送?字节需要是URL安全的Base64编码,因此正确的Java方法可能应该是Base64。encodeBase64URLSafeString()
String encodedImage = Base64.encodeBase64URLSafeString(response.getContent());