如何通过REST API在ruby中进行身份验证 [英] How to authenticate in ruby via REST api
问题描述
所以到目前为止,我一直在使用Google auth lib(在其他Google服务中):
require'googleauth'
scopes = [https://www.googleapis.com/auth/firebase.database]
tt = Google :: Auth.get_application_default(scopes).fetch_access_token
我得到tt [access_token]然后我做一些类似于
curl -X POST -d'{user_id:jack,text:Ahoy!}''https://project-xxxxxxxxxxxxxxxxxxx.firebaseio.com /message_list.json?access_token=ya29.CjHzAsjcGEQsSppI9AKCV9g4Uen7qlREI3N_nCGDsWkLh6ZSCg5JmNYzIU8NuV6PAq7h'
就像他们在 https://firebase.google.com/docs/reference/rest/database/user-auth#section-get 我得到: 我得到了: 我应该如何验证Firebase WITHOUT nodejs ...只是一个简单的REST ??? 请协助! 谢谢! 假设你有一个如上所述的访问令牌: 您可以编辑 I am trying to authenticate with ruby to firebase as Service Account.
So what I did so far, is using Google auth lib as I always did (in other google services): I get the tt["access_token"] and then I do something like Like they say in https://firebase.google.com/docs/reference/rest/database/user-auth#section-get and I get:
Permission denied (that is because in our DB Rules we said auth != null.
So I went ahead and called And I got: How am I supposed to authenticate to Firebase WITHOUT nodejs... just a simple REST??? Please assist! Thanks! Here's a fully baked example, assuming you have an access token as described above: You can edit 这篇关于如何通过REST API在ruby中进行身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
权限被拒绝(这是因为在我们的DB规则中,我们说auth!= null。
所以我去了前面叫做?auth = ya29.CjHzAsjcGEQsSppI9AKCV9g4Uen7qlREI3N_nCGDsWkLh6ZSCg5JmNYzIU8NuV6PAq7h
{\ n \error \:\无法解析授权令牌。\\\\
} \ n
require'cgi'
要求'json'
要求'net / https'
要求'uri'
ACCESS_TOKEN ='XXX'
DATABASE_NAME ='YYY'
path ='/ example'
auth_variable = {uid:'ruby-admin'}
payload = {my:'data'}
uri = URI https://#{DATABASE_NAME} .firebaseio.com#{path} .json?auth_variable_override =#{CGI :: escape auth_variable.to_json})
req = Net :: HTTP :: Post.new(uri,'Content-Type'=> 'application / json')
req.body = payload.to_json
req ['Authorization'] =Bearer#{ACCESS_TOKEN}
http = Net :: HTTP.new(uri.hostname,uri.port)
http.use_ssl = true
res = http.request(req)
puts res。 body
auth_variable
auth
安全规则中可用的变量,而 payload
显然是您要发布的数据。require 'googleauth'
scopes = ["https://www.googleapis.com/auth/firebase.database"]
tt = Google::Auth.get_application_default(scopes).fetch_access_token
curl -X POST -d '{"user_id" : "jack", "text" : "Ahoy!"}' 'https://project-xxxxxxxxxxxxxxxxxxx.firebaseio.com/message_list.json?access_token=ya29.CjHzAsjcGEQsSppI9AKCV9g4Uen7qlREI3N_nCGDsWkLh6ZSCg5JmNYzIU8NuV6PAq7h'
?auth=ya29.CjHzAsjcGEQsSppI9AKCV9g4Uen7qlREI3N_nCGDsWkLh6ZSCg5JmNYzIU8NuV6PAq7h
"{\n \"error\" : \"Could not parse auth token.\"\n}\n"
require 'cgi'
require 'json'
require 'net/https'
require 'uri'
ACCESS_TOKEN = 'XXX'
DATABASE_NAME = 'YYY'
path = '/example'
auth_variable = {uid: 'ruby-admin'}
payload = {my: 'data'}
uri = URI("https://#{DATABASE_NAME}.firebaseio.com#{path}.json?auth_variable_override=#{CGI::escape auth_variable.to_json}")
req = Net::HTTP::Post.new(uri, 'Content-Type' => 'application/json')
req.body = payload.to_json
req['Authorization'] = "Bearer #{ACCESS_TOKEN}"
http = Net::HTTP.new(uri.hostname, uri.port)
http.use_ssl = true
res = http.request(req)
puts res.body
auth_variable
to change the auth
variable available in security rules, and payload
is obviously your data you want to post.