I'm trying to watch for changes in Google Drive, and getting 401 Exception. Searching here, i found that people have detailed messages, why they are not unauthorized though i have nothing.
Here the code i use:
public static void main(String[] args) { try { httpTransport = GoogleNetHttpTransport.newTrustedTransport(); dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR); // authorization GoogleCredential credential = GoogleCredential.getApplicationDefault() .createScoped(DriveScopes.all()); boolean refreshed = credential.refreshToken(); // set up the global Drive instance drive = new Drive.Builder(httpTransport, JSON_FACTORY, credential) .setApplicationName(APPLICATION_NAME) .build(); Channel channel = new Channel(); channel.setId(UUID.randomUUID().toString()); channel.setType("web_hook"); //my ip here channel.setAddress("https://com.example:"); StartPageToken pageToken = drive.changes().getStartPageToken().execute(); System.out.println(pageToken.getStartPageToken()); Channel changesChannel = drive.changes().watch(pageToken.getStartPageToken(), channel).execute(); System.out.println(changesChannel.getExpiration()); return; } catch (IOException e) { System.err.println(e.getMessage()); e.printStackTrace(); } catch (Throwable t) { t.printStackTrace(); } System.exit(1); } More info:
- This is a service account. It has Owner permissions
- I'm using it from local computer
- drive.files().list() works fine
- drive.about().get() works fine
And the exception i'm getting:
com.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:145) at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113) at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40) at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:321) at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1065) at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419) at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352) at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469) at main.lambda.GoogleDrive2.main(MyClass.java:142)
my pom.xml dependencies:
<dependency> <groupId>com.google.api-client</groupId> <artifactId>google-api-client</artifactId> <version>1.20.0</version> </dependency> <dependency> <groupId>com.google.oauth-client</groupId> <artifactId>google-oauth-client</artifactId> <version>1.22.0</version> </dependency> <!-- https://mvnrepository.com/artifact/com.google.api-client/google-api-client-extensions --> <dependency> <groupId>com.google.api-client</groupId> <artifactId>google-api-client-extensions</artifactId> <version>1.6.0-beta</version> </dependency> <dependency> <groupId>com.google.oauth-client</groupId> <artifactId>google-oauth-client-jetty</artifactId> <version>1.22.0</version> </dependency> <dependency> <groupId>com.google.apis</groupId> <artifactId>google-api-services-drive</artifactId> <version>v3-rev70-1.22.0</version> </dependency>