How to authenticate with ruby ​​via REST api

I am trying to authenticate from ruby ​​to firebase as a service account. So what I have done so far uses 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 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'

As stated in https://firebase.google.com/docs/reference/rest/database/user-auth#section-get

and I get: Permission denied (this is because in our database 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 can I go through Firebase authentication WITHOUT nodes ... just just REST ???

Please, help!

Thank!

+1
source share
1

, , , :

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, auth, , payload - , , , .

+3

Source: https://habr.com/ru/post/1649366/


All Articles