Android-Google云端硬盘HTTP请求

cra*_*ool 5 java android google-docs-api google-drive-api

我正在尝试编写一个可以将文件上传到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*
Run Code Online (Sandbox Code Playgroud)

我试图使用以下方法在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);
Run Code Online (Sandbox Code Playgroud)

我的应用程序中有一个有效的令牌,并且我也尝试添加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;
    }
Run Code Online (Sandbox Code Playgroud)

我的令牌应该有效,因为我可以通过访问来获取用户信息

"https://www.googleapis.com/oauth2/v1/userinfo?access_token=" + token
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

编辑:对于任何寻求帮助的人,我都无法解决,但不能完全确定问题是什么,但是最后我使用了官方api-可以在此处找到一个很好的工作示例 Google drive SDK 2.0抛出错误400 Bad Request

Jer*_*ome 1

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

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