Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8748099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:28:01+00:00 2026-06-13T12:28:01+00:00

I am trying to write an application which can upload files to Google Drive.

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T12:28:02+00:00Added an answer on June 13, 2026 at 12:28 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write an TUI bandwidth trace application which on query can
I am trying to write a Qt GUI application which can communicate with an
Hi i am trying to write an application which deals with .p12 files In
I'm trying to write an application which uses Google's protocol buffers to deserialize data
I have a GWT application for which I'm trying to write some tests using
I am trying to write a windows service application which deals with storing values
I have the following in my Rails 3 controllers/application.rb (which I'm trying to write
I'm trying to write a Cocoa application which allows me to see what buttons
I've been trying to write a small application which will work with mysql in
I am trying to write an android tablet application which scans for a set

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.