1

我正在使用 0.6.4 版的 google-api-client ruby​​ gem 来查询 Google Admin Directory API。

这是我在 ruby​​ 控制台中的会话:

require 'rubygems'
require 'google/api_client'

SERVICE_ACCOUNT_CLIENT_ID = "SOME_STRING.apps.googleusercontent.com"
SERVICE_ACCOUNT_EMAIL = "SOME_STRING@developer.gserviceaccount.com"
SERVICE_ACCOUNT_PKCS12_FILE_PATH = "/path/to/privatekey.p12"

key = Google::APIClient::KeyUtils.load_from_pkcs12(SERVICE_ACCOUNT_PKCS12_FILE_PATH, 'notasecret')
asserter = Google::APIClient::JWTAsserter.new(SERVICE_ACCOUNT_EMAIL, "https://www.googleapis.com/auth/admin.directory.device.chromeos.readonly", key)
client = Google::APIClient.new
client.authorization = asserter.authorize
dir_api = client.discovered_api('admin', 'directory_v1')
resp = client.execute(:api_method => dir_api.chromeosdevices.list, :parameters => {'customerId'=>SERVICE_ACCOUNT_CLIENT_ID})

resp.body
=> "{\n \"error\": {\n  \"errors\": [\n   {\n    \"domain\": \"global\",\n    \"reason\": \"badRequest\",\n    \"message\": \"Bad Request\"\n   }\n  ],\n   \"code\": 400,\n  \"message\": \"Bad Request\"\n }\n}\n"

我可能在这里遗漏了一些明显的东西,但从错误响应中不清楚请求中缺少什么。将不胜感激任何正确方向的帮助/指示。

谢谢。

4

3 回答 3

2

我在我的 C# 程序中得到了类似的响应,列出了我域中的用户。

我仍然没有解决它,但到目前为止, 当我尝试执行相同的请求时,我设法在 APIs Explorer https://developers.google.com/apis-explorer/#p/admin/directory_v1/中收到相同的错误消息那里并省略域的任何值。

这让我相信我需要以某种方式在我的 C# 代码中将域添加到我的请求中,并且通过反复试验,我认为我找到了如何在 C# 中做到这一点。

但是我的建议是在 APIs Explorer 中尝试您的 API 请求,然后通过不提交域的值来确定您是否可以在那里获得相同的错误消息,因为我认为这就是错误消息的含义。

于 2013-07-21T15:44:54.783 回答
0

I've had the same problems. I've wrote an example gist which explains how to set it up:

https://gist.github.com/thomaswitt/7468182

Steps are:

  1. Go to Google Cloud Console (https://cloud.google.com/console)
  2. Create Service Account with P12 File
  3. Enable the Admin SDK in APIs.
  4. Create a Project
  5. Create a registered app within this project
  6. Go to section 'Certificate' and generate a key
  7. Download the JSON file as well
  8. Go to the Apps Console > Security > Extended > 3rdPartgy OAuth (https://admin.google.com/AdminHome?#OGX:ManageOauthClients)
  9. Add an API Client. Client name is value of client_id in the JSON file, API Scope is https://www.googleapis.com/auth/admin.directory.user.readonly
于 2013-11-14T14:57:51.323 回答
0

我发现@JoBe 的回答非常正确。在 Ruby 中,使用Google-Api-Clientgem 时,您需要list_users使用domain. IE

UserService.list_users(domain: 'mydomain.com').

于 2019-06-10T23:02:02.903 回答