如何使用scribe-java将新文件插入Google云端硬盘

时间:2012-07-09 01:55:01

标签: oauth scribe google-drive-api

最近,我正在尝试使用scribe-java(https://github.com/fernandezpablo85/scribe-java)将Google Drive集成到我的应用程序中。 并通过Id方法列出文件/获取文件工作正常。但我无法插入新的File方法正常工作。 这是我的代码(插入):

OAuthService service = new ServiceBuilder()
                .provider(GoogleApi.class)
                .apiKey("[my api key]")
                .apiSecret("[my api secret]")
                .scope(SCOPE)
                .callback("[my callback]")
                .build();
Token token = new Token(accessToken, accessSecret);
OAuthRequest request = new OAuthRequest(Verb.POST, "https://www.googleapis.com/drive/v2/files");
service.signRequest(token, request);
Response response = request.send();

这将在我的google驱动器中创建一个“无标题”命名文件,我的问题是如何从本地文件系统上传我的文件并指定名称?因为我尝试添加添加体参数,添加标题,它们都无法正常工作。非常感谢

1 个答案:

答案 0 :(得分:1)

您正在获取“无标题”命名文件,因为您没有使用POST请求发送任何内容。 您可以通过此documentation找到有关Google Drive API的媒体上传协议的详细信息。

以下示例显示了分段上传方法的协议:

POST https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart

Authorization: Bearer <Access token>
Content-Length: 44332
Content-Type: multipart/related; boundary="===============0124852474844509178=="

===============0124852474844509178==
Content-Type: application/json

{'title': 'My Image'}
===============0124852474844509178==
Content-Type: image/png

<Binary data removed>
===============0124852474844509178==

我不熟悉Scribe,但您可能想尝试使用Google APIs client library for Java来抽象强类型类中的所有协议交互。 Google云端硬盘API的reference guide包含使用此客户端库的代码段。