我正在尝试编写一个可以将文件上传到Google云端硬盘的应用程序。我选择通过原始http请求与此服务进行交互,因为我没有在Android上找到任何有用的API示例,因为它似乎比提供的库更轻量级。
我使用https://developers.google.com/oauthplayground/来获取各种调用和响应,并尝试编写一个简单的请求来返回文件列表。通过该工具发送的请求是:
POST /drive/v2/files HTTP/1.1
Host: www.googleapis.com
Content-length: 0
Content-type: application/json
Authorization: OAuth *token goes here*
我试图使用以下方法在eclipse中复制它:
URL url = new URL("https://www.googleapis.com/drive/v2/files?v=3");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Host:", "www.googleapis.com");
conn.setRequestProperty("Content-length:", "0");
conn.setRequestProperty("Content-type:", "application/json");
conn.setRequestProperty("Authorization:", "OAuth " + token);
我的应用程序中有一个有效的令牌,我也尝试添加标题conn.setRequestProperty("GData-Version:", "3.0")
如Google文档中所述。我收到了响应代码400,我确信我的代码有一些明显的错误,但是我没有在我看过这些看起来正确的示例之前和之后编写头请求。
任何帮助都将不胜感激。
编辑: 好的,我还有进一步的 - 我现在得到一个401 - 有一条消息说我需要登录 编辑:我现在从服务器收到“已接收的身份验证质询为空”响应。
String token = fetchToken();
if (token == null) {
return;
}
URL url = new URL("https://www.googleapis.com/drive/v2/files");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
try {
conn.setRequestMethod("POST"); //use post method
conn.setDoOutput(true); //we will send stuff
conn.setDoInput(true); //we want feedback
conn.setUseCaches(false); //no caches
conn.setAllowUserInteraction(false);
conn.setRequestProperty("HTTP-Version:", "HTTP/1.1");
conn.setRequestProperty("Content-type: ", "application/json");
conn.setRequestProperty("Authorization:","OAuth" + token);
}
catch (ProtocolException e)
{
e.printStackTrace();
}
OutputStream out = conn.getOutputStream();
try {
OutputStreamWriter wr = new OutputStreamWriter(out);
wr.write("{\"Method\":\"POST\",\"absoluteURI\"" +
":\"https://www.googleapis.com/drive/v2/files\"," +
"\"headers\":{\"Content-Type\":\"application/json\"," +
"\"Content-Length\":\"0\"},\"message-body\":\"\"," +
"\"access_token\":\"" +
token +
"\"" +
"," +
"\"access_token_type\":\"oauth\"}"
); //ezm is my JSON object containing the api commands
wr.flush();
wr.close();
}
catch (IOException e) {
}
finally { //in this case, we are ensured to close the output stream
if (out != null)
out.close();
}
int sc = conn.getResponseCode();
if (sc == 200) {
Log.i(TAG, "200 OK..");
InputStream is = conn.getInputStream();
String name = getFileName(readResponse(is));
System.out.println(readResponse(is));
//mActivity.show("Hello " + name + "!");
is.close();
return;
} else if (sc == 401) {
GoogleAuthUtil.invalidateToken(mActivity, token);
onError("Server auth error, please try again.", null);
Log.i(TAG, "Server auth error: " + readResponse(conn.getErrorStream()));
return;
} else {
System.out.println(sc);
onError("Server returned the following error code: " + sc, null);
return;
}
我的令牌应该有效,因为我可以通过访问
来获取用户信息"https://www.googleapis.com/oauth2/v1/userinfo?access_token=" + token
有什么想法吗?
编辑: 对于任何找到这个寻求帮助的人,我从来没有让它工作,不完全确定问题是什么,但最后我使用官方api - 一个好的工作示例可以在这里找到 Google drive SDK 2.0 throws error 400 Bad Request
答案 0 :(得分:1)
您确定您的令牌对于驱动器范围有效吗?您在问题结尾处提到的测试不在同一范围内。
另外,不要忘记Oauht2令牌仅在一小时内有效。您可能需要申请另一个或使用刷新令牌(如果有的话)。
答案 1 :(得分:0)
如果您使用HTTPS,则使用GET而不是POST和Bearer令牌。
使用curl:
curl -H "Authorization: Bearer {TOKEN}" \
-H "Content-Type: application/json" \
https://www.googleapis.com/drive/v2/files
我不知道为什么谷歌没有记录REST API。如果您在某个地方找到了文档,请发表评论。