Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a user with the Admin Directory api using the google-api-ruby-client?

I've been trying a few combinations but can't seem to come up with something that works. More information about the API I'm asking about can be found here https://developers.google.com/admin-sdk/directory/v1/reference/users/insert . I have a feeling I'm just not setting up the request correctly. The following bit of code is known to work. I use it to set up the client that is able to query all the users.

client = Google::APIClient.new(:application_name => "myapp", :version => "v0.0.0")
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 => issuer,
     :signing_key => key,
     :person => user + "@" + domain)
client.authorization.fetch_access_token!
api = client.discovered_api("admin", "directory_v1")

When I try to use the following code

parameters = Hash.new
parameters["password"] = "ThisIsAPassword"
parameters["primaryEmail"] = "tstacct2@" + domain
parameters["name"] = {"givenName" => "Test", "familyName" => "Account2"}
parameters[:api_method] = api.users.insert
response = client.execute(parameters)

I always get back the same error "code": 400, "message": "Invalid Given/Family Name: FamilyName"

I've observed a few things while looking into this particular API. If I print out the parameters for both the list and the insert functions e.g

puts "--- Users List ---"
puts api.users.list.parameters
puts "--- Users Insert ---"
puts api.users.insert.parameters

Only the List actually displays the parameters

--- Users List ---
  customer
  domain
  maxResults
  orderBy
  pageToken
  query
  showDeleted
  sortOrder
--- Users Insert ---

This leaves me wondering if the ruby client is unable to retrieve the api and therefore unable to submit the request correctly or if I'm just doing something completely wrong.

I'd appreciate any idea's or direction that might help set me on the right path.

Thank you,

James

like image 641
James Woodward Avatar asked Aug 27 '13 04:08

James Woodward


1 Answers

You need to supply an Users resource in the request body, which is also the reason why you don't see it in the params. So the request should look like:

# code dealing with client and auth

api = client.discovered_api("admin", "directory_v1")

new_user = api.users.insert.request_schema.new({
  'password' => 'aPassword',
  'primaryEmail' => '[email protected]',
  'name' => {
    'familyName' => 'John',
    'givenName' => 'Doe'
  }
})

result = client.execute(
  :api_method => api.users.insert,
  :body_object => new_user
)
like image 113
Silvano Avatar answered Nov 05 '22 06:11

Silvano