3

I am trying to insert a user account through this api using gem 'google-api-client 0.11.1'.

I have done the following:

  1. Enabled API access
  2. Enabled service account domain wide delegation
  3. Authorized the scope urls for this service account's client id

The code works with the other domain, but for the other account it throws this error:

{
 "error": "unauthorized_client",
 "error_description": "Client is unauthorized to retrieve access tokens using this method."
}

Here's my code:

SCOPES = ['https://www.googleapis.com/auth/admin.directory.user'
, 'https://www.googleapis.com/auth/admin.directory.group.readonly']
    ENV['GOOGLE_APPLICATION_CREDENTIALS'] = 'expa-email-c2938bce41f9.json'
 service = Google::Apis::AdminDirectoryV1::DirectoryService.new
  authorization = Google::Auth.get_application_default(SCOPES)
  authorization.sub = user_email
  authorization.fetch_access_token!
  service.authorization = authorization
  service

How can I resolve this?

Chris
  • 1,206
  • 2
  • 15
  • 35
Anbazhagan p
  • 943
  • 1
  • 14
  • 27

1 Answers1

0

Based from this thread, a service account is a dummy user. For a service account to be able to access your account it needs to be preauthorized. You authorize the service account just like you would any other user. You may check this sample code.

Additional reference:

abielita
  • 13,147
  • 2
  • 17
  • 59