CJG
CJG

Reputation: 558

Connecting Rails Application to Firebase Cloud Firestore

Would Like to read Data from the Firebase Cloud Firestore. I have already created a project, and added my Rails (6.0.3) application to the fire store db via settings -> add web app. This provided me with script tags containing the firevbase SDK to add to my web application under the body tag, I did all of this. I am trying to follow the instructions @ https://firebase.google.com/docs/firestore/quickstart?authuser=0#ruby. The very first instruction reads: ''' export GOOGLE_APPLICATION_CREDENTIALS="path/to/your/keyfile.json" ''' I have already downloaded this JSON file by creating a key from the service account associated with my firebase project, this key was downloaded and placed within my project tree. However I do not know where to put Export google creds within my Rails environment? Previously I tried to insert it into environment/development.rb but the export key word is not a part of Ruby syntax.

I have already installed the cloud-firestore-gem and I have been working through this post trying to piece everything together How to connect to a Firestore database with Ruby. When I run this within the rails console:

cred = Google::Cloud::Firestore::Credentials.new("path/to/myJsonSerivcesKey.Json") 

I receive this trace back :

Signet::AuthorizationError (Authorization failed.  Server message:)
{"error":"invalid_scope","error_description":"Invalid OAuth scope or ID token audience provided."}

I think this error has to do with me not doing something correct when using the API key from the service account but I am not really sure. Any advice on how to correct the Invalid OAuth Scope and where to put the export GOOGLE_APPLICATIONS+CREDENTIALS would be great. Also I am not sure why Firebase SDK needs to be pasted into the body tags of the application, could someone explain what this does for the app?

Upvotes: 3

Views: 2128

Answers (2)

Doug
Doug

Reputation: 15515

You need to pass in the scope you want:

cred = Google::Cloud::Firestore::Credentials.new "path/to/myJsonSerivcesKey.Json",  scope: "https://www.googleapis.com/auth/datastore"

List of scopes available

Upvotes: 1

David Friedl
David Friedl

Reputation: 11

I had the same issue but was also using google-cloud-storage besides google-cloud-firestore.

What fixed it for me was downgrading the cloud-storage gem by changing my gem file to gem 'google-cloud-storage', '<= 1.21.1'

Upvotes: 1

Related Questions