在ruby中使用google api客户端发送请求时,如何解决验证错误“您提交的请求无效”

时间:2019-07-25 16:02:41

标签: ruby api google-api-client double-click-advertising

我在ruby上使用google API客户端连接到DoubleClick Bid Manager API。在本地我收到此错误:

Google::Apis::ClientError: validationError: The request you submitted is invalid.

我一步一步地遵循了文档,并且尝试了Google提供的文档页面上的API。对于某些请求,我在本地获得了成功的响应,但是对于其他请求,我得到了validationError,但我不知道为什么。当我使用Google提供的文档页面尝试使用相同的参数进行相同的请求时,它起作用了。

这就是我使用API的方式。我正在使用服务帐户进行授权:

service = Google::Apis::DoubleclickbidmanagerV1::DoubleClickBidManagerService.new

scopes = ['https://www.googleapis.com/auth/doubleclickbidmanager', 'https://www.googleapis.com/auth/dfareporting']
  authorization = Google::Auth.get_application_default(scopes)
service = Google::Apis::DoubleclickbidmanagerV1::DoubleClickBidManagerService.new
service.authorization = authorization
service.authorization.fetch_access_token!

service.download_line_items
service.get_query(123)

预期结果必须为200 OK,但我得到了:

400
#<HTTP::Message:0x00007f89c46ea960 @http_header=#<HTTP::Message::Headers:0x00007f89c46ea938 @http_version="1.1", @body_size=0, @chunked=false, @request_method="GET", @request_uri=#<Addressable::URI:0x3fc4e2375aa0 URI:https://www.googleapis.com/doubleclickbidmanager/v1/query/625649848?>, @request_query=nil, @request_absolute_uri=nil, @status_code=400, @reason_phrase="Bad Request", @body_type=nil, @body_charset=nil, @body_date=nil, @body_encoding=#<Encoding:UTF-8>, @is_request=false, @header_item=[["Vary", "Origin"], ["Vary", "X-Origin"], ["Content-Type", "application/json; charset=UTF-8"], ["Content-Encoding", "gzip"], ["Date", "Thu, 25 Jul 2019 15:59:38 GMT"], ["Expires", "Thu, 25 Jul 2019 15:59:38 GMT"], ["Cache-Control", "private, max-age=0"], ["X-Content-Type-Options", "nosniff"], ["X-Frame-Options", "SAMEORIGIN"], ["X-XSS-Protection", "1; mode=block"], ["Server", "GSE"], ["Alt-Svc", "quic=\":443\"; ma=2592000; v=\"46,43,39\""], ["Transfer-Encoding", "chunked"]], @dumped=false>, @peer_cert=#<OpenSSL::X509::Certificate: subject=#<OpenSSL::X509::Name CN=*.googleapis.com,O=Google LLC,L=Mountain View,ST=California,C=US>, issuer=#<OpenSSL::X509::Name CN=Google Internet Authority G3,O=Google Trust Services,C=US>, serial=#<OpenSSL::BN:0x00007f89ce108718>, not_before=2019-06-18 08:35:41 UTC, not_after=2019-09-10 08:16:00 UTC>, @http_body=#<HTTP::Message::Body:0x00007f89c46ea848 @body="{\n \"error\": {\n  \"errors\": [\n   {\n    \"domain\": \"global\",\n    \"reason\": \"validationError\",\n    \"message\": \"The request you submitted is invalid.\"\n   }\n  ],\n  \"code\": 400,\n  \"message\": \"The request you submitted is invalid.\"\n }\n}\n", @size=0, @positions=nil, @chunk_size=nil>, @previous=nil>
Caught error validationError: The request you submitted is invalid.
Error - #<Google::Apis::ClientError: validationError: The request you submitted is invalid. status_code: 400 header: #<HTTP::Message::Headers:0x00007f89ce1027c8 @http_version="1.1", @body_size=0, @chunked=false, @request_method="GET", @request_uri=#<Addressable::URI:0x3fc4e2375aa0 URI:https://www.googleapis.com/doubleclickbidmanager/v1/query/625649848?>, @request_query=nil, @request_absolute_uri=nil, @status_code=400, @reason_phrase="Bad Request", @body_type=nil, @body_charset=nil, @body_date=nil, @body_encoding=#<Encoding:UTF-8>, @is_request=false, @header_item=[["Vary", "Origin"], ["Vary", "X-Origin"], ["Content-Type", "application/json; charset=UTF-8"], ["Content-Encoding", "gzip"], ["Date", "Thu, 25 Jul 2019 15:59:38 GMT"], ["Expires", "Thu, 25 Jul 2019 15:59:38 GMT"], ["Cache-Control", "private, max-age=0"], ["X-Content-Type-Options", "nosniff"], ["X-Frame-Options", "SAMEORIGIN"], ["X-XSS-Protection", "1; mode=block"], ["Server", "GSE"], ["Alt-Svc", "quic=\":443\"; ma=2592000; v=\"46,43,39\""], ["Transfer-Encoding", "chunked"]], @dumped=false> body: "{\n \"error\": {\n  \"errors\": [\n   {\n    \"domain\": \"global\",\n    \"reason\": \"validationError\",\n    \"message\": \"The request you submitted is invalid.\"\n   }\n  ],\n  \"code\": 400,\n  \"message\": \"The request you submitted is invalid.\"\n }\n}\n">

Google::Apis::ClientError: validationError: The request you submitted is invalid.
from /Users/heron/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/google-api-client-0.30.7/lib/google/apis/core/http_command.rb:228:in `check_status'

0 个答案:

没有答案