google-admin-sdk - 如何使用 google-api-ruby-client 创建具有管理目录 api 的用户?

标签 google-admin-sdk google-api-ruby-client

我一直在尝试一些组合,但似乎无法想出可行的方法。有关我所询问的 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")

当我尝试使用以下代码时

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)

我总是得到相同的错误“code”:400,“message”:“无效的名字/姓氏:FamilyName”

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

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

只有List真正显示参数

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

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

如果有任何想法或方向可以帮助我走上正确的道路,我将不胜感激。

谢谢,

詹姆斯

最佳答案

您需要提供Users resource在请求正文中,这也是您在参数中看不到它的原因。 所以请求应该如下所示:

# 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
)

关于google-admin-sdk - 如何使用 google-api-ruby-client 创建具有管理目录 api 的用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18457099/

相关文章:

amazon-web-services - 对同一域使用 Amazon SES 和 Google GSuite

ruby-on-rails - 域范围委派后出现 notFound 错误

ruby-on-rails - Google Oauth SSL错误-SSL_connect返回= 1 errno = 0状态= SSLv3读取服务器证书B : certificate verify failed

java - 使用 Google Admin Directory API 添加组 - 403 无权访问此资源/API

c# - Google 管理员目录用户错误

python - Google Groups API 添加成员

ruby - Google API ruby​​ 客户端显然没有获取所有事件

ruby - 在 Google Drive API 中收到错误 403 - 超出了未经身份验证使用的每日限制。继续使用需要注册

Ruby 实时谷歌分析 API

node.js - users.list 返回 403 错误 : Not Authorized to access this resource/api