我正在尝试编写一个可以将文件上传到 Google Drive 的应用程序。我选择通过原始 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