gpt4 book ai didi

java - 使用 REST API 将图像文件上传到 Google Drive 时出错

转载 作者:行者123 更新时间:2023-11-29 18:30:54 25 4
gpt4 key购买 nike

我是第一次使用 Google 云端硬盘。我正在尝试通过我的应用程序将 jpg 文件上传到我的 Google 云端硬盘。我已完成OAuth 2.0账号登录和驱动权限授权。我已按照此处给出的说明成功将文件上传到 Google Drive https://developers.google.com/drive/api/v3/manage-uploads?refresh=1

问题出在上传的文件上。图像没有作为图像保存在那里。这里的请求体应该是什么形式?

这是我用来使用 Google REST API 上传图像文件的代码 fragment 。

OkHttpClient client = new OkHttpClient();
RequestBody body = RequestBody.create(MediaType.parse("application/json"), file);
Request request = new Request.Builder()
.url("https://www.googleapis.com/upload/drive/v3/files?uploadType=media")
.addHeader("Content-Type", "image/jpeg")
.addHeader("Content-Length", "36966.4")
.addHeader("Authorization", String.format("Bearer %s", accessToken))
.post(body)
.build();
Response response = null;
try {
response = client.newCall(request).execute();
successCode = String.valueOf(response.code());
}catch (IOException e){
e.printStackTrace();
}

这里的“file”是图片的Base64编码字符串。

它只是给出了预期的 http ok 200 代码。还需要知道如何在上传到 Google Drive 时设置文件的标题。

最佳答案

您在请求中提到了错误的内容类型。应该是

RequestBody body = RequestBody.create(MediaType.parse("image/jpeg"), file);

关于java - 使用 REST API 将图像文件上传到 Google Drive 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56339470/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com