繁体   English   中英

Google Cloud Storage在Ruby中无故返回400错误请求

[英]Google Cloud Storage returning 400 bad request without reason in Ruby

我在Ruby on Rails应用程序中使用HTTParty gem向我的Google Cloud Storage存储桶发出请求。 Google返回状态码400,表示“错误请求”,但未给出错误原因。 我已验证授权不是问题。 这是我的代码:

module Google
  class Client
    include HTTParty
    base_uri "https://www.googleapis.com"

    def get_pics
     self.class.get("/storage/v1/b/bucket-name/o?key=<%= ENV['GOOGLE_KEY'] %>")
    end

  end
end

然后在控制器中:

class WelcomeController < ApplicationController
  def index
    response = Google::Client.new.get_pics
  end
end

这是我得到的答复:

#<HTTParty::Response:0x7f8826f71268 parsed_response="<HTML>\n<HEAD>\n<TITLE>Bad Request</TITLE>\n</HEAD>\n<BODY 
BGCOLOR=\"#FFFFFF\" TEXT=\"#000000\">\n<H1>Bad Request</H1>\n<H2>Error 400</H2>\n</BODY>\n</HTML>\n", @response=#<Net::HTTPBadRequest 
400 Bad Request readbody=true>, @headers={"content-type"=>["text/html; 
charset=UTF-8"], "content-length"=>["145"], "date"=>["Thu, 12 Nov 2015 
04:02:14 GMT"], "expires"=>["Thu, 12 Nov 2015 04:02:14 GMT"], 
"cache-control"=>["private, max-age=0"], "x-content-type-options"=>
["nosniff"], "x-frame-options"=>["SAMEORIGIN"], "x-xss-protection"=>["1; mode=block"], 
"server"=>["GSE"], "alternate-protocol"=>["443:quic,p=1"], 
"alt-svc"=>["quic=\":443\"; p=\"1\"; ma=604800"], "connection"=>["close"]}>

这可能有很多事情,但是最可能的原因是您的密钥无效。 您的密钥无效的最常见原因是您不小心使用了服务帐户的“密钥ID”,而不是实际的“ API密钥”(它们很容易混淆)。

要生成API密钥,请访问http://console.developers.google.com ,打开工具栏,选择“ API Manager”,然后选择“ Credentials”页面。 忽略“服务帐户密钥”部分下的所有内容,即使显示“密钥ID”也是如此。 这些是服务帐户密钥。

而是单击“新凭据”,然后选择“ API密钥”。 在弹出窗口中选择“服务器密钥”或“浏览器密钥”(如果要从Rails服务器使用服务器密钥,请在运行客户端的浏览器中使用服务器密钥)。 现在,尝试使用该新密钥。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM