2012-10-24 107 views
4

我正在尝试编写可将文件上传到Google云端硬盘的应用程序。我选择通过原始http请求与此服务进行交互,因为我没有在Android上找到任何有用的API示例,因为它似乎比提供的库更轻量级。Android - Google Drive HTTP请求

我已经使用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

回答

1

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

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

+0

这是有效的 - tbh我不完全确定问题是什么,但我解决了这个问题,通过使用本文中的示例实现官方API http://stackoverflow.com/questions/12888795/google-drive-sdk -2-0抛出错误-400-坏请求 – crazyfool

0

如果你使用HTTPS,GET而不是POST和一个持证者令牌,它可以工作。

使用curl:

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

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