Android – Google Drive HTTP请求

我正在尝试编写一个可以将文件上传到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 – 有一条消息说我需要登录编辑:我现在收到来自服务器的“收到的身份validation质询为空”响应。

  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 – 一个很好的工作示例可以在这里找到谷歌驱动器SDK 2.0抛出错误400错误请求

您确定您的令牌对于Drive Scopes有效吗? 您在问题结尾处提到的测试不在同一范围内。

另外,不要忘记Oauht2令牌仅在一小时内有效。 您可能需要请求另一个或使用刷新令牌(如果有)。

如果您使用HTTPS,则使用GET而不是POST和Bearer令牌。

使用curl:

 curl -H "Authorization: Bearer {TOKEN}" \ -H "Content-Type: application/json" \ https://www.googleapis.com/drive/v2/files 

我不知道谷歌为什么不记录REST API。 如果您在某个地方找到了文档,请发表评论。