How to create a user using the admin directory api using google-api-ruby-client?

I tried several combinations, but can't come up with something that works. More information about the API I'm asking for can be found here https://developers.google.com/admin-sdk/directory/v1/reference/users/insert . I have a feeling that I just didn’t configure the request correctly. The next bit of code is known to work. I use it to configure a client that can request all 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 return the same error "code": 400, "message": "Invalid Given / Family Name: FamilyName"

I have observed several things when considering this particular API. If I print the parameters for both the list and the insert functions, for example,

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

Only the list actually displays the options

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

This makes me wonder if the ruby ​​client could not extract the api and therefore would not be able to send the request correctly or if I was just doing something completely wrong.

I would appreciate any idea or direction that could help me on the right track.

Thanks,

James

+4
source share
1 answer

You need to provide a user resource in the request body, which is also the reason that you do not see it in the parameters. Therefore, the query should look like this:

 # code dealing with client and auth api = client.discovered_api("admin", "directory_v1") new_user = api.users.insert.request_schema.new({ 'password' => 'aPassword', 'primaryEmail' => ' testAccount@myDomain.mygbiz.com ', 'name' => { 'familyName' => 'John', 'givenName' => 'Doe' } }) result = client.execute( :api_method => api.users.insert, :body_object => new_user ) 
+7
source

Source: https://habr.com/ru/post/1499066/


All Articles