[英]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.