【问题标题】:Authenticating to the Google Admin Directory API对 Google Admin Directory API 进行身份验证
【发布时间】:2018-05-28 01:48:31
【问题描述】:

我正在尝试弄清楚如何对 Admin Directory API 进行身份验证。我的目标是能够创建新的 GSuite 用户。

我已按照本指南 https://github.com/jay0lee/GAM/wiki/CreatingClientSecretsFile 设置了具有域范围委派的客户端/ID 机密和服务帐户。

我可以成功获得不记名令牌,但是当我尝试向端点发出请求时,我得到 403。我希望我应该通过此端点的身份验证,因为我可以使用 GAM 成功获取所有数据,它正在使用相同的凭据。

require 'googleauth'
require 'google/apis/admin_directory_v1'

scope = ["https://www.googleapis.com/auth/admin.directory.user.readonly", "https://www.googleapis.com/auth/admin.directory.user"]

authorizer = Google::Auth::ServiceAccountCredentials.make_creds(
  json_key_io: File.open('service_account.json'),
  scope: scope)

pload = authorizer.fetch_access_token!
token = pload["access_token"]

url = "https://www.googleapis.com/admin/directory/v1/users/my@email.com"
uri = URI.parse(url)
request = Net::HTTP::Get.new(uri)
request.content_type = "application/json"
request["Authorization"] = "Bearer #{token}"

req_options = {
  use_ssl: uri.scheme == "https",
}

response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
  http.request(request)
end

p response #=> <Net::HTTPForbidden 403 Forbidden readbody=true>

【问题讨论】:

  • 它说的具体错误是什么?

标签: google-admin-sdk


【解决方案1】:

修复了服务帐户的范围,一切正常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-14
    • 2019-02-17
    • 1970-01-01
    • 2013-06-24
    • 2020-02-11
    • 1970-01-01
    • 2017-03-11
    • 2013-11-23
    相关资源
    最近更新 更多