找不到404资源:使用Google Directory API的域
我跟着快速入门,尝试使用google-api-ruby-client创建用户。
我在google api控制台中设置了访问权限。 我可以使用API资源管理器来实现这一点。
但是,当我尝试使用ruby客户端时,我得到的资源未找到:域错误。
这是代码:
def self.create_user # Initialize the client. client = Google::APIClient.new( :application_name => 'MYAPP', :application_version => '0.0.1' ) # Authorization # Load our credentials for the service account key = Google::APIClient::KeyUtils.load_from_pkcs12(KEY_FILE, KEY_SECRET) 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: ACCOUNT_ID, signing_key: key) # Request a token for our service account client.authorization.fetch_access_token! # Load API Methods admin = client.discovered_api('admin', 'directory_v1') # Make an API call. result = client.execute( admin.users.update, name: { familyName: 'testy', givenName: 'testerson' }, password: '!password12345!', primaryEmail: 'ttesterson@my-actual-domain.com' ) result.data
结束
这是回复:
"error"=>{"errors"=>[{"domain"=>"global", "reason"=>"notFound", "message"=>"Resource Not Found: domain"}], "code"=>404, "message"=>"Resource Not Found: domain"}
为什么?
在阅读了一些文档之后,我需要解决两件事。
- 我没有为我的测试服务帐户设置适当的授权。
您必须转到应用控制台>安全性>高级>管理API客户端访问,并为您的服务帐户添加客户端URL以及要添加的任何特定权限
- 如此问题所示,您似乎需要创建用户对象而不是仅传入参数。
这是我更新的代码:
# Authorization happens here .... api = client.discovered_api('admin', 'directory_v1') new_user = api.users.insert.request_schema.new( name: { familyName: 'Testy', givenName: 'Testerson' }, primaryEmail: 'ttttesterson@.com', password: 'password123' ) result = client.execute( api_method: api.users.insert, body_object: new_user )