我正在开发 GAE java 应用程序,我需要将 PDF 文件上传到用户的谷歌驱动器,但上传总是失败,我不知道什么可能是错误的,因为我已经检查了几个代码示例。
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.model.File;
.
.
.
byte[] pdfContent = <... 52kb PDF file ...>
ByteArrayContent mediaContent = new ByteArrayContent("application/pdf", pdfContent);
File myPDF = new File();
myPDF.setTitle("Sample PDF");
myPDF.setDescription("Sample PDF");
Drive driveService = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, getStoredCredential()).build();
Drive.Files.Insert insert = driveService.files().insert(hugePDF, mediaContent);
insert.getMediaHttpUploader().setChunkSize(1024 * 1024);
File file = insert.execute();
此上传代码总是得到错误:400 OK
{
"error": {
"errors": [
{
"domain": "global",
"reason": "badRequest",
"message": "Bad Request"
}
],
"code": 400,
"message": "Bad Request"
}
}
我正在使用 Google 驱动器库:google-api-services-drive-v2-rev1-1.7.2-beta.jar
凭据很好,因为我可以从用户的谷歌驱动器列出或下载文件。