404 Resource Not Found: domain with Google Directory API
Asked Answered
H

1

2

I followed the quick start and am attempting to create a user using the google-api-ruby-client.

I've set up access in the google api console. And I can get this to work using the API explorer.

But when I try using the ruby client, I'm getting a resource not found: domain error.

Here's the code:

def self.create_user

# Initialize the client.
client = Google::APIClient.new(
  :application_name => 'MYAPP',
  :application_version => '0.0.1'
)

# Authorization
# Load our credentials for the service account
key = Google::APIClient::KeyUtils.load_from_pkcs12(KEY_FILE, KEY_SECRET)

client.authorization = Signet::OAuth2::Client.new(
  token_credential_uri: 'https://accounts.google.com/o/oauth2/token',
  audience: 'https://accounts.google.com/o/oauth2/token',
  scope: 'https://www.googleapis.com/auth/admin.directory.user',
  issuer: ACCOUNT_ID,
  signing_key: key)

# Request a token for our service account
client.authorization.fetch_access_token!

# Load API Methods
admin = client.discovered_api('admin', 'directory_v1')

# Make an API call.
result = client.execute(
   admin.users.update,
    name: { familyName: 'testy', givenName: 'testerson' },
     password: '!password12345!',
     primaryEmail: '[email protected]'
)

result.data

end

Here's the response:

"error"=>{"errors"=>[{"domain"=>"global", "reason"=>"notFound", "message"=>"Resource Not Found: domain"}], "code"=>404, "message"=>"Resource Not Found: domain"}

Why?

Hasseman answered 20/5, 2014 at 19:8 Comment(1)
Are you still running this application? Do you have any experience with the new Ruby API? I cannot get the example to work because of apparently missing permissions.Madeleinemadelena
H
1

After a bit of documentation reading, there were two things that I needed to fix.

  1. I hadn't set up the proper authorization for my test service account.

You have to go to the Apps Console > Security > Advanced > Manage API client access and add the client url for your service account as well as any specific permissions that you want to add

  1. As seen in this question, it seems that you need to create a user object rather than just passing in parameters.

Here's my updated code:

# Authorization happens here ....

api = client.discovered_api('admin', 'directory_v1')
new_user = api.users.insert.request_schema.new(
    name: { familyName: 'Testy', givenName: 'Testerson' },
    primaryEmail: 'ttttesterson@<domain-redacted>.com',
    password: 'password123'
 )

  result = client.execute(
    api_method: api.users.insert,
    body_object: new_user
 )
Hasseman answered 5/6, 2014 at 18:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.