如何通过REST API在ruby中进行身份验证 [英] How to authenticate in ruby via REST api

查看:198
本文介绍了如何通过REST API在ruby中进行身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用红宝石认证作为服务帐户的Firebase。
所以到目前为止,我一直在使用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

我得到:
权限被拒绝(这是因为在我们的DB规则中,我们说auth!= null。
所以我去了前面叫做?auth = ya29.CjHzAsjcGEQsSppI9AKCV9g4Uen7qlREI3N_nCGDsWkLh6ZSCg5JmNYzIU8NuV6PAq7h



我得到了:

 {\ n \error \:\无法解析授权令牌。\\\\
} \ n

我应该如何验证Firebase WITHOUT nodejs ...只是一个简单的REST ???



请协助!

谢谢!

解决方案

假设你有一个如上所述的访问令牌:

  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 显然是您要发布的数据。


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):

require 'googleauth'
scopes =  ["https://www.googleapis.com/auth/firebase.database"]
tt = Google::Auth.get_application_default(scopes).fetch_access_token

I get the tt["access_token"] and then I do something like

curl -X POST -d '{"user_id" : "jack", "text" : "Ahoy!"}' 'https://project-xxxxxxxxxxxxxxxxxxx.firebaseio.com/message_list.json?access_token=ya29.CjHzAsjcGEQsSppI9AKCV9g4Uen7qlREI3N_nCGDsWkLh6ZSCg5JmNYzIU8NuV6PAq7h'

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 ?auth=ya29.CjHzAsjcGEQsSppI9AKCV9g4Uen7qlREI3N_nCGDsWkLh6ZSCg5JmNYzIU8NuV6PAq7h

And I got:

"{\n  \"error\" : \"Could not parse auth token.\"\n}\n"

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:

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

You can edit auth_variable to change the auth variable available in security rules, and payload is obviously your data you want to post.

这篇关于如何通过REST API在ruby中进行身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆