Google-api-ruby-client: Trouble Creating Users

Created on 16 Feb 2016  路  3Comments  路  Source: googleapis/google-api-ruby-client

I'm trying to generate user accounts with the API. I've run the sample code that does "list users" and get the first 10 users in my domain. When I try to do an insert with a test user object, I get back the "Invalid Given/Family Name". I can't figure out what I'm doing wrong here....

# Initialize the API
service = Google::Apis::AdminDirectoryV1::DirectoryService.new
service.client_options.application_name = APPLICATION_NAME
service.authorization = authorize
# List the first 10 users in the domain
user_object = {
  'primaryEmail' => '[email protected]',
  'name' => {
    'givenName' => 'Test',
    'familyName' => 'User'
  },
  'password' => initial_password,
  'changePasswordAtNextLogin' => true
}

response = service.insert_user(user_object)
puts response
exit

The error I get is this...

$ bundle exec ./prep_account.rb
/Users/matt/.chefdk/gem/ruby/2.1.0/gems/google-api-client-0.9/lib/google/apis/core/http_command.rb:202:in `check_status': invalid: Invalid Given/Family Name: FamilyName (Google::Apis::ClientError)
    from /Users/matt/.chefdk/gem/ruby/2.1.0/gems/google-api-client-0.9/lib/google/apis/core/api_command.rb:103:in `check_status'
    from /Users/matt/.chefdk/gem/ruby/2.1.0/gems/google-api-client-0.9/lib/google/apis/core/http_command.rb:170:in `process_response'
    from /Users/matt/.chefdk/gem/ruby/2.1.0/gems/google-api-client-0.9/lib/google/apis/core/http_command.rb:275:in `execute_once'
    from /Users/matt/.chefdk/gem/ruby/2.1.0/gems/google-api-client-0.9/lib/google/apis/core/http_command.rb:107:in `block (2 levels) in execute'
    from /Users/matt/.chefdk/gem/ruby/2.1.0/gems/retriable-2.1.0/lib/retriable.rb:54:in `block in retriable'
    from /Users/matt/.chefdk/gem/ruby/2.1.0/gems/retriable-2.1.0/lib/retriable.rb:48:in `times'
    from /Users/matt/.chefdk/gem/ruby/2.1.0/gems/retriable-2.1.0/lib/retriable.rb:48:in `retriable'
    from /Users/matt/.chefdk/gem/ruby/2.1.0/gems/google-api-client-0.9/lib/google/apis/core/http_command.rb:104:in `block in execute'
    from /Users/matt/.chefdk/gem/ruby/2.1.0/gems/retriable-2.1.0/lib/retriable.rb:54:in `block in retriable'
    from /Users/matt/.chefdk/gem/ruby/2.1.0/gems/retriable-2.1.0/lib/retriable.rb:48:in `times'
    from /Users/matt/.chefdk/gem/ruby/2.1.0/gems/retriable-2.1.0/lib/retriable.rb:48:in `retriable'
    from /Users/matt/.chefdk/gem/ruby/2.1.0/gems/google-api-client-0.9/lib/google/apis/core/http_command.rb:96:in `execute'
    from /Users/matt/.chefdk/gem/ruby/2.1.0/gems/google-api-client-0.9/lib/google/apis/core/base_service.rb:267:in `execute_or_queue_command'
    from /Users/matt/.chefdk/gem/ruby/2.1.0/gems/google-api-client-0.9/generated/google/apis/admin_directory_v1/service.rb:3108:in `insert_user'
    from ./prep_account.rb:62:in `<main>'
triage me

Most helpful comment

Hi! I know this issue is closed, but i'm facing the same problem and the solutions suggested are not working for me.

The following solution works:

user_object = Google::Apis::AdminDirectoryV1::User.new
user_object.primary_email = '[email protected]'
user_object.name = Google::Apis::AdminDirectoryV1::UserName.new
user_object.name.given_name = 'Test'
user_object.name.family_name = 'User'
user_object.password = initial_password
user_object.change_password_at_next_login = true

Thanks.

All 3 comments

The ruby client uses snake case (as per ruby convention) in the generated interfaces.

# Initialize the API
service = Google::Apis::AdminDirectoryV1::DirectoryService.new
service.client_options.application_name = APPLICATION_NAME
service.authorization = authorize
# List the first 10 users in the domain
user_object = {
  'primary_email' => '[email protected]',
  'name' => {
    'given_name' => 'Test',
    'family_name' => 'User'
  },
  'password' => initial_password,
  'change_password_at_next_login' => true
}

response = service.insert_user(user_object)
puts response
exit

Ah thanks....I thought I'd tried that. Actually discovered you have to use the symbols, not just snake case strings which still didn't work.

As in...

user_object = {
  :primary_email => '[email protected]',
  :name => {
    :given_name => 'Test',
    :family_name => 'User'
  },
  :password => initial_password,
  :change_password_at_next_login => true
}

response = service.insert_user(user_object)
puts response

Also found the User and UserName class in the Representations file and that helped as well...

https://github.com/google/google-api-ruby-client/blob/master/generated/google/apis/admin_directory_v1/representations.rb

Hi! I know this issue is closed, but i'm facing the same problem and the solutions suggested are not working for me.

The following solution works:

user_object = Google::Apis::AdminDirectoryV1::User.new
user_object.primary_email = '[email protected]'
user_object.name = Google::Apis::AdminDirectoryV1::UserName.new
user_object.name.given_name = 'Test'
user_object.name.family_name = 'User'
user_object.password = initial_password
user_object.change_password_at_next_login = true

Thanks.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

majioa picture majioa  路  6Comments

brandonhilkert picture brandonhilkert  路  6Comments

davidraj picture davidraj  路  6Comments

jponc picture jponc  路  5Comments

arjunmenon picture arjunmenon  路  5Comments