tofi9
tofi9

Reputation: 5853

Google drive SDK 2.0 throws error 400 Bad Request

I've been fighting with Google Drive API for Android for more than 50 hours now, and have not come one inch closer. From my understanding, there are 1001 ways to access Google drive (Google Docs API, REST & Google Drive SDK v2). I'm using Google Drive SDK v2. I want want to access Google Drive to upload jpeg files. Platform, Android 2.2+.

What I've tried:

My code:

Account account = AccountManager.get(context).getAccountsByType(
        "com.google")[0];

String token;
try {
    token = GoogleAuthUtil.getToken(context, account.name, "oauth2:"
            + DriveScopes.DRIVE_FILE);
} catch (UserRecoverableAuthException e) {
    context.startActivityForResult(e.getIntent(), ASK_PERMISSION);
    return;
} catch (IOException e) {
    return;
} catch (GoogleAuthException e) {
    return;
}

HttpTransport httpTransport = new NetHttpTransport();
JacksonFactory jsonFactory = new JacksonFactory();
Drive.Builder b = new Drive.Builder(httpTransport, jsonFactory, null);
final String tokenCopy = token;
b.setJsonHttpRequestInitializer(new JsonHttpRequestInitializer() {
    public void initialize(JsonHttpRequest request) throws IOException {
        DriveRequest driveRequest = (DriveRequest) request;
        driveRequest.setPrettyPrint(true);
        driveRequest
                .setKey("1234567890-abcdefghij123klmnop.apps.googleusercontent.com");
        driveRequest.setOauthToken(tokenCopy);
    }
});

final Drive drive = b.build();
FileList files;
try {
    files = drive.files().list().setQ("mimeType=text/plain").execute();
} catch (IOException e) {
    e.printStackTrace(); // throws HTTP 400
}

The error I'm getting is:

com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request
{
  "code" : 400,
  "errors" : [ {
    "domain" : "global",
    "location" : "q",
    "locationType" : "parameter",
    "message" : "Invalid Value",
    "reason" : "invalid"
  } ],
  "message" : "Invalid Value"
}

Upvotes: 1

Views: 4487

Answers (1)

Jerome
Jerome

Reputation: 2184

As the error message suggests, your error is in the query parameter q. The correct syntax for your q parameter is

files = drive.files().list().setQ("mimeType='text/plain'").execute();

and not :

files = drive.files().list().setQ("mimeType=text/plain").execute();

Looking at your code, you are fully authenticated and your request is failing because of this syntax error.

Upvotes: 1

Related Questions