I used Service Account key with scope (https://www.googleapis.com/auth/admin.directory.user.readonly) to access Google Directory API Users.
With the same setup as below, I can access Users List API (link).
But when trying to make request to Users Watch API (link), Status 403 returned.
These 2 APIs required the same scope user.readonly that my SA key has.
My domain has already been verified and added into Domain verification screen on GCP Console.
val httpTransport = GoogleNetHttpTransport.newTrustedTransport()
val jsonFactory = JacksonFactory()
val inputStream = HealthController::class.java.getResourceAsStream("/credentials.json")
?: throw FileNotFoundException("/credentials.json")
val credential = GoogleCredential.fromStream(inputStream, httpTransport, jsonFactory)
.toBuilder()
.setServiceAccountScopes(listOf(DirectoryScopes.ADMIN_DIRECTORY_USER_READONLY))
.setServiceAccountUser("admin@example.com")
.build()
service = Directory.Builder(httpTransport, jsonFactory, credential)
.setApplicationName("Some Name")
.build()
val channel = Channel()
channel.address = "https://example.com/webhook/v1/google/users"
channel.expiration = Instant.now().toEpochMilli() + 6 * 60 * 60 * 1000
channel.id = "webhook001"
channel.token = "abcxyz"
channel.type = "web_hook"
channel.payload = false
val result = service.users().watch(channel)
.setDomain("example.com")
.setViewType("domain_public")
.execute()
I logged the issue to Google issue tracker: https://issuetracker.google.com/issues/171300784