Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Google Drive HTTP Request

I am trying to write an application which can upload files to Google Drive. I have chosen to interact with this service via raw http requests as I haven't found any useful examples of the API on Android and as it seems more lightweight then the provided libraries.

I've used https://developers.google.com/oauthplayground/ to get the various calls and responses and am trying to write a simple request to return the list of files. The request sent via the tool is:

POST /drive/v2/files HTTP/1.1
Host: www.googleapis.com
Content-length: 0
Content-type: application/json
Authorization: OAuth *token goes here*

I have tried to replicate this in eclipse using:

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);

I have a valid token in my application and I have also tried to add the header conn.setRequestProperty("GData-Version:", "3.0") as specified in the Google documentation. I'm getting the response code 400, I'm sure there is something obvious wrong with my code but I haven't written header requests before and from the examples I've seen this looks correct.

Any help would be appreciated.

EDIT: Ok I have got further - I am now getting a 401 - with a message saying I need to login EDIT: I am now getting a "Received authentication challenge is null" response from the server.

   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;
    }

My token should be valid as I can get the user information by accessing

"https://www.googleapis.com/oauth2/v1/userinfo?access_token=" + token

Any ideas?

EDIT: For anyone finding this looking for help I never got it to work, not entirely sure what the issue was but in the end I used the official api - a good working example can be found here Google drive SDK 2.0 throws error 400 Bad Request

like image 678
crazyfool Avatar asked Oct 24 '12 18:10

crazyfool


1 Answers

Are you sure your token is valid for Drive Scopes ? The test you mention at the end of your question is not on the same scope.

Also, don't forget that Oauht2 token are valid only for one hour. You may need to request another one or to use your refresh token, if you have one.

like image 150
Jerome Avatar answered Sep 30 '22 14:09

Jerome