如何使用google-api-ruby-client使用Admin Directory api创建用户?

Jam*_*ard 4 google-api-ruby-client google-admin-sdk

我一直在尝试一些组合,但似乎无法想出一些有用的东西.有关我要问的API的更多信息,请访问https://developers.google.com/admin-sdk/directory/v1/reference/users/insert.我有一种感觉,我只是没有正确设置请求.已知下面的代码可以工作.我用它来设置能够查询所有用户的客户端.

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")
Run Code Online (Sandbox Code Playgroud)

当我尝试使用以下代码时

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)
Run Code Online (Sandbox Code Playgroud)

我总是得到相同的错误"代码":400,"消息":"无效的给定/姓氏:FamilyName"

在研究这个特定的API时,我发现了一些事情.如果我打印出列表和插入函数的参数,例如

puts "--- Users List ---"
puts api.users.list.parameters
puts "--- Users Insert ---"
puts api.users.insert.parameters
Run Code Online (Sandbox Code Playgroud)

只有列表实际显示参数

--- Users List ---
  customer
  domain
  maxResults
  orderBy
  pageToken
  query
  showDeleted
  sortOrder
--- Users Insert ---
Run Code Online (Sandbox Code Playgroud)

这让我想知道ruby客户端是否无法检索api因此无法正确提交请求或者我是否只是做了一些完全错误的事情.

我很欣赏任何可能有助于让我走上正确道路的想法或方向.

谢谢,

詹姆士

小智 7

您需要在请求正文中提供Users资源,这也是您在params中看不到它的原因.因此请求应如下所示:

# 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
)
Run Code Online (Sandbox Code Playgroud)