2013-08-27 34 views
4

我一直在嘗試一些組合,但似乎無法想出有用的東西。關於我所問的API的更多信息可以在這裏找到https://developers.google.com/admin-sdk/directory/v1/reference/users/insert。我有一種感覺,我只是沒有正確設置請求。下面的代碼已知可以工作。我用它來設置能夠查詢所有用戶的客戶端。如何使用google-api-ruby-client創建具有Admin Directory API的用戶?

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"] = "[email protected]" + domain 
parameters["name"] = {"givenName" => "Test", "familyName" => "Account2"} 
parameters[:api_method] = api.users.insert 
response = client.execute(parameters) 

我總是得到相同的錯誤 「代碼」:400, 「消息」: 「無效鑑於/姓:FamilyName」

我在觀察這個特定的API時觀察了一些東西。如果我打印出來的參數都在列表和插件功能,例如

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

只有列表實際顯示參數

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

這讓我想知道是否紅寶石客戶端是無法檢索API因此無法正確提交請求,或者我只是在做一些完全錯誤的事情。

我會很感激任何想法或方向,可能會幫助我在正確的道路上。

謝謝

詹姆斯

回答

7

您需要在請求體,這也是爲什麼你沒有看到它在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' => '[email protected]', 
    'name' => { 
    'familyName' => 'John', 
    'givenName' => 'Doe' 
    } 
}) 

result = client.execute(
    :api_method => api.users.insert, 
    :body_object => new_user 
) 
+0

我應該已經意識到,從使用Drive API。感謝您的幫助。我按照你的例子成功創建了一個用戶。 –

相關問題