NeyLive
NeyLive

Reputation: 417

Where do I put the Google API .json key file?

I am trying to integrate Google Calendar API into my Rails app, so I can create Google calendar events from my app.

Google has provided a client_secret.json file. I can use it locally, but now have no idea how to implement the key file on heroku.

Can anyone help me with this?

Upvotes: 1

Views: 1275

Answers (1)

trh
trh

Reputation: 7339

It should just be a secret and key (the names and values).

For heroku, you can set them as environment variables. Like so:

heroku config:set GOOGLE_SECRET=8N029N81ASLDKFA823_WO4ANF

(but please use the information provided in your json file)

Now. The next step is to make sure that your application only calls for those keys in a single way. An option is to use the config/secrets.yml file. Call to the environment variable in the secrets file. Then, you can use the dotenv gem in development so you have .env file in your application's root where you place all your env vars (but do NOT check in the .env file)

Create a file if it doesn't exist config/secrets.yml and inside it add:

 production:
   google_secret: ENV['GOOGLE_SECRET']

 development:
   google_secret: ENV['GOOGLE_SECRET']

(obviously, again, make sure the name is what you choose)

Next add dotenv to your gem file and run bundle

gem 'dotenv-rails', :groups => [:development, :test]

Create a new file called .env in the app's root. And add your variable directly including both the key name and the value.

GOOGLE_SECRET=8N029N81ASLDKFA823_WO4ANF

You can add as many env variables as you need to.

Now edit the .gitignore file and add the .env file to it, to make sure you don't accidentally check in your private information.

Lastly, in any place that you need to USE the environment variable, is to call it from secrets like so:

Rails.application.secrets.google_secret

I know it seems like a lot of steps, but it will ensure that you dev matches your prod without making a lot of if Rails.env.development? statements in your code.

Upvotes: 2

Related Questions