向 Google 管理目录 API 进行身份验证
Authenticating to the Google Admin Directory API
我正在尝试了解如何对管理目录进行身份验证 API。我的目标是能够创建新的 GSuite 用户。
我已按照本指南https://github.com/jay0lee/GAM/wiki/CreatingClientSecretsFile设置了一个 client/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>
修复了服务帐户的范围,一切正常。
我正在尝试了解如何对管理目录进行身份验证 API。我的目标是能够创建新的 GSuite 用户。
我已按照本指南https://github.com/jay0lee/GAM/wiki/CreatingClientSecretsFile设置了一个 client/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>
修复了服务帐户的范围,一切正常。