【问题标题】:Android - Google Drive HTTP RequestAndroid - Google Drive HTTP 请求
【发布时间】:2012-10-24 18:56:41
【问题描述】:

我正在尝试编写一个可以将文件上传到 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

【问题讨论】:

    标签: java android google-docs-api google-drive-api


    【解决方案1】:

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

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

    【讨论】:

    【解决方案2】:

    如果你使用 HTTPS,GET 而不是 POST 和 Bearer 令牌,它可以工作。

    使用卷曲:

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

    我不知道为什么 google 没有记录 REST API。如果您在某处找到文档,请发表评论。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-06
      • 1970-01-01
      • 2011-09-06
      相关资源
      最近更新 更多