0
votes

Is the Google Drive API sample command line app (DriveCommandLine) currently known to work? I have downloaded the Java client library, and compiled the sample application (inserting my application's client ID and secret created by specifying "installed application" and "other"). It runs and informs me to access the following link in a browser:

https://accounts.google.com/o/oauth2/auth?access_type=online&approval_prompt=auto&client_id=MY_ACTUAL_CLIENT_ID&redirect_uri=urn:ietf:wg:oauth:2.0:oob&response_type=code&scope=https://www.googleapis.com/auth/drive

(Obviously with my actual client ID there.)

This successfully provides me with an authentication code. When I enter this at the application's prompt, then at the line:

File file = service.files().insert(body, mediaContent).execute();

I get the exception:

Exception in thread "main" com.google.api.client.http.HttpResponseException: 401 Unauthorized
    at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:978)
    at com.google.api.client.googleapis.media.MediaHttpUploader.executeUploadInitiation(MediaHttpUploader.java:338)
    at com.google.api.client.googleapis.media.MediaHttpUploader.upload(MediaHttpUploader.java:252)
    at com.google.api.services.drive.Drive$Files$Insert.executeUnparsed(Drive.java:309)
    at com.google.api.services.drive.Drive$Files$Insert.execute(Drive.java:331)
    at DriveCommandLine.main(DriveCommandLine.java:56)

Investigation shows that the GoogleCredential returned has all elements set to null (which seems wrong).

The version of the Drive API client that I am using is "v2-rev5-1.7.2-beta", which is the one currently pointed to on the quickstart page here: https://developers.google.com/drive/quickstart

I am compiling and running with this JAR and all the ones in the "libs" subdirectory in the classpath (the quickstart page refers to adding source JARs to the classpath which, obviously, dosn't work).

Can anyone shed any light on what might be going wrong here?

Thanks!

2

2 Answers

0
votes

The link in the Quick Start is outdated. Try downloading the Drive SDK from the following link: http://code.google.com/p/google-api-java-client/wiki/APIs#Drive_API

See if that makes a difference for you. I have the DriveCommandLine app running fine.

0
votes
  • You need to download JSON file from the API access page at https://cloud.google.com/console.

  • Rename this json file as "client_secrets.json"

  • Copy this json into your project folder or more specifically copy it into "src/main/resources". (This file contains the clientID and secrets). If the file is already present in the folder then replace it.

  • Now run the project. You will be redirect to Google Drive's "allow access" page.

PS - json file must belong to an "Installed Type Application" because this is a commandline sample and runs locally.