本书中的每一个错误都来自Google-Api-Ruby-Client,但没有数据

发布于 2025-01-28 01:50:43 字数 4475 浏览 4 评论 0原文

我本周一直在尝试根据老板的要求进行工作,该请求需要使用Google Admin Directory API。

在这一点上,我质疑我要做什么甚至有可能。

  • 我可以从范围“ https://www.googleapis.com/auth/admin.directory.device.device.mobile.readonly”中检索数据吗?有可能吗?

我过去一个小时看到的错误在下面... 他们中的许多听起来都一样,我不知道发生了什么,或者为什么这是如此艰难的旅程。

  1. 许可证_DENIED:请求没有足够的身份验证范围。 (Google :: apis :: clienterror)

  2. `check_status':未经授权(Google :: Apis :: aperizationerror)

  3. 授权失败。服务器消息:( Signet ::授权) { “错误”:“ unuthorized_client”, “ error_description”:“客户端未经授权以使用此方法检索访问令牌,或者未针对所请求的任何范围授权客户端。” }

  4. `check_status':cermission_denied:请求不足身份验证范围

  5. “ check_status':badrequest:bad request

我的当前测试脚本在下面...

require "google/apis/admin_directory_v1"
require "googleauth"
require "googleauth/stores/file_token_store"
require "fileutils"

APPLICATION_NAME = "Directory API Ruby Quickstart".freeze
CREDENTIALS_PATH = "credentials.json".freeze
CUSTOMER_ID = "thasgunnabeanopefrommedawg".freeze

SCOPE = ["https://www.googleapis.com/auth/admin.directory.device.mobile.readonly"].freeze

authorizer = Google::Auth::ServiceAccountCredentials.make_creds(
  json_key_io: 
File.open('credentials.json'),
  scope: SCOPE)
authorizer.update!(sub: "[email protected]")
authorizer.fetch_access_token!

# puts authorize

# Initialize the API
service = Google::Apis::AdminDirectoryV1::DirectoryService.new
service.client_options.application_name = APPLICATION_NAME
service.authorization = Google::Auth.get_application_default(SCOPE)

response = service.list_mobile_devices(customer_id: CUSTOMER_ID)
puts response.to_json

在*** [27th,5月27日,2022年5月27日]

我一直在与Ruby,Python和Postman一起尝试了两个星期此时:/

昨晚我拿了用户发布的Ruby片段:Daimto以下。

我能够使用以下答案中提供的Ruby片段的以下修改版本返回令牌。

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

creds = {
  "type": "service_account",
  "project_id": "MYPROJECTNAME",
  "private_key_id": "MYPRIVATEKEYID",
  "private_key": "-----BEGIN PRIVATE KEY-----\n-MY PRIVATE KEY 
WILL BE HERE BUT REMOVED FOR SECURITY-----END PRIVATE KEY-----\n",
"client_email": "[email protected]",
  "client_id": "MYCLIENTIDISACTUALLYHERE",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/compute%40developer.gserviceaccount.com"
}

creds_json = creds.to_json
creds_json_io = StringIO.new(creds_json)
auth = Google::Auth::ServiceAccountCredentials.make_creds(
  json_key_io: creds_json_io, 
scope["https://www.googleapis.com/auth/admin.directory.device.mobile.readonly","https://www.googleapis.com/auth/admin.directory.device.chromeos.readonly","https://www.googleapis.com/auth/admin.directory.device.mobile"]
)
    auth.sub = "emailfrommyserviceaccount- 
   [email protected]" 

puts auth.fetch_access_token

请原谅格式。

我现在将服务帐户从ENV变量中取出,以确保我可以在此时添加额外的抽象层的层次。

尝试将其他代码从目录API QuickStart添加到上述sinip时,我仍然返回错误

/var/lib/gems/2.7.0/gems/google-apis-core-core-0.5.5.0/lib/google /pis/core/http_command.rb:224:in“ check_status':未经授权(google :: apis :: apis :: perutherizationerror)

添加的附加代码添加在下面... 上一个剪辑的最后一行更改为此后剪切的第一行。修改用户:daimto的响应后,这是为了将令牌正确传递给示例。

authorize = auth.fetch_access_token

# Initialize the API
service = Google::Apis::AdminDirectoryV1::DirectoryService.new
service.client_options.application_name = "my-application-name"
service.authorization = authorize
# List the first 10 users in the domain
response = service.list_users(customer:    "my_customer",
                              max_results: 10,
                              order_by:    "email")
puts "Users:"
puts "No users found" if response.users.empty?
response.users.each { |user| puts "- #{user.primary_email} (#{user.name.full_name})" }

I have been attempting to work on a request from my boss this week that requires using the google admin directory api.

At this point I am questioning if what I am trying to do is even possible.

  • Can I retrieve data from the scope "https://www.googleapis.com/auth/admin.directory.device.mobile.readonly" with a service account? Is it even possible?

The errors I have seen in the past hour are below...
Many of them sound the same and I have no idea what is going on or why this is such a difficult journey for such basic information.

  1. PERMISSION_DENIED: Request had insufficient authentication scopes. (Google::Apis::ClientError)

  2. `check_status': Unauthorized (Google::Apis::AuthorizationError)

  3. Authorization failed. Server message: (Signet::AuthorizationError)
    {
    "error": "unauthorized_client",
    "error_description": "Client is unauthorized to retrieve access tokens using this method, or client not authorized for any of the scopes requested."
    }

  4. `check_status': permission_denied: request had insufficient authentication scopes

  5. `check_status': badRequest: Bad Request

My current test script is below...

require "google/apis/admin_directory_v1"
require "googleauth"
require "googleauth/stores/file_token_store"
require "fileutils"

APPLICATION_NAME = "Directory API Ruby Quickstart".freeze
CREDENTIALS_PATH = "credentials.json".freeze
CUSTOMER_ID = "thasgunnabeanopefrommedawg".freeze

SCOPE = ["https://www.googleapis.com/auth/admin.directory.device.mobile.readonly"].freeze

authorizer = Google::Auth::ServiceAccountCredentials.make_creds(
  json_key_io: 
File.open('credentials.json'),
  scope: SCOPE)
authorizer.update!(sub: "[email protected]")
authorizer.fetch_access_token!

# puts authorize

# Initialize the API
service = Google::Apis::AdminDirectoryV1::DirectoryService.new
service.client_options.application_name = APPLICATION_NAME
service.authorization = Google::Auth.get_application_default(SCOPE)

response = service.list_mobile_devices(customer_id: CUSTOMER_ID)
puts response.to_json

EDITS BELOW *** [27th, MAY, 2022]

I have been trying with ruby, python, and postman for two weeks at this point :/

Last night I took the ruby snippet that was posted by user:Daimto below.

I was able to return a token with the following modified version of the ruby snippet provided in the answer below.

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

creds = {
  "type": "service_account",
  "project_id": "MYPROJECTNAME",
  "private_key_id": "MYPRIVATEKEYID",
  "private_key": "-----BEGIN PRIVATE KEY-----\n-MY PRIVATE KEY 
WILL BE HERE BUT REMOVED FOR SECURITY-----END PRIVATE KEY-----\n",
"client_email": "[email protected]",
  "client_id": "MYCLIENTIDISACTUALLYHERE",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/compute%40developer.gserviceaccount.com"
}

creds_json = creds.to_json
creds_json_io = StringIO.new(creds_json)
auth = Google::Auth::ServiceAccountCredentials.make_creds(
  json_key_io: creds_json_io, 
scope["https://www.googleapis.com/auth/admin.directory.device.mobile.readonly","https://www.googleapis.com/auth/admin.directory.device.chromeos.readonly","https://www.googleapis.com/auth/admin.directory.device.mobile"]
)
    auth.sub = "emailfrommyserviceaccount- 
   [email protected]" 

puts auth.fetch_access_token

Please excuse the formatting.

I took the service account out of the env variable for now to make sure I can get it to work without adding extra layers of abstraction at this time.

When trying to add the additional code from the Directory Api Quickstart to the above snip I STILL RETURN THE ERROR

/var/lib/gems/2.7.0/gems/google-apis-core-0.5.0/lib/google/apis/core/http_command.rb:224:in `check_status': Unauthorized (Google::Apis::AuthorizationError)

The additional code added is below...
The last line of the previous snip gets changed to the first line of the snip that comes after this. This is to properly pass the token to the example after modifying user:Daimto's response.

authorize = auth.fetch_access_token

# Initialize the API
service = Google::Apis::AdminDirectoryV1::DirectoryService.new
service.client_options.application_name = "my-application-name"
service.authorization = authorize
# List the first 10 users in the domain
response = service.list_users(customer:    "my_customer",
                              max_results: 10,
                              order_by:    "email")
puts "Users:"
puts "No users found" if response.users.empty?
response.users.each { |user| puts "- #{user.primary_email} (#{user.name.full_name})" }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

世界等同你 2025-02-04 01:50:43

方法

为了回答您的第一个问题是的,您可以使用https://www.googleapis.com/auth/admin/admin.directory.device.mobile.mobile.readonly scope。

错误编号1

PERMISSION_DENIED: Request had insufficient authentication scopes.

时,当您提供了不同的范围时,您可能会遇到此错误。

错误3;

 Client is unauthorized to retrieve access tokens using this method, or client not authorized for any of the scopes requested.

您可以在Google Cloud Console上创建三种类型的客户端。

  • Web Client
  • 本地客户端
  • 服务帐户

您创建这些客户端获得的JSON文件都是不同的。使用它们的代码也不同。错误告诉您您使用的是您正在使用的client.json文件,该文件与所使用的代码类型不匹配。

如何创建服务帐户credetinals

服务帐户的代码将就像没有测试,您可能需要修复范围。请记住,服务帐户需要在工作区域域上正确配置以使子工作正常工作。

require 'googleauth'
require 'google/apis/admin_v1'

creds = ENV['GOOGLE_SERVICE_ACCOUNT'] # JSON downloaded from cloud console
                                      # is saved in this ENV variable
creds_json = JSON.parse(creds)
creds_json_io = StringIO.new(creds_json.to_json)
auth = Google::Auth::ServiceAccountCredentials.make_creds(
  json_key_io: creds_json_io,
  scope: [Google::Apis::ADMINV1::ADMIN_DIRECTORY_MOBILE_READONLY]
)
auth.sub = '[email protected]' 
auth.fetch_access_token

提示:您那里有很多错误,我觉得您已经为此挣扎了一段时间。建议退后一步,看看 google-api-ruby -client 。重新开始。只需让您的身材上班即可。一旦您正确完成代码,并且客户端正确,所有零件都可以安装到位。

The method Method: mobiledevices.list requires one of the following scopes.

enter image description here

So to answer your first question yes you can use the https://www.googleapis.com/auth/admin.directory.device.mobile.readonly scope.

Error number 1

PERMISSION_DENIED: Request had insufficient authentication scopes.

You were probably getting this error when you had supplied a different scope.

Error 3;

 Client is unauthorized to retrieve access tokens using this method, or client not authorized for any of the scopes requested.

There are three types of clients you can create on google cloud console.

  • web client
  • native client
  • service account

The json file you get from creating these clients is all different. The code that uses them is also different. The error is telling you that you have a client.json file that you are using which does not match the type of code you are using.

How to create service account credetinals

The code for a service account would be like this Not tested you may need to fix the scope. Remember that the service account needs to be configured properly on your workspace domain for the sub to work.

require 'googleauth'
require 'google/apis/admin_v1'

creds = ENV['GOOGLE_SERVICE_ACCOUNT'] # JSON downloaded from cloud console
                                      # is saved in this ENV variable
creds_json = JSON.parse(creds)
creds_json_io = StringIO.new(creds_json.to_json)
auth = Google::Auth::ServiceAccountCredentials.make_creds(
  json_key_io: creds_json_io,
  scope: [Google::Apis::ADMINV1::ADMIN_DIRECTORY_MOBILE_READONLY]
)
auth.sub = '[email protected]' 
auth.fetch_access_token

Tip: You have a lot of errors there, I feel that you have been struggling with this for a while. Advice step back, have a look at the sample on the readme for the Google-api-ruby-client. Start over. Just get your auth to work. Once you get the code right and the client right all the pieces will fit into place.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文