2

Have not played with Rails in ages so walking through the Learn Ruby on Rails tutorial which is excellent.

I'm having issues with google authentication, the example code calls the config/secrets.yml variables (which are read from ENV in the shell) from the app/models/contact.rb model update_spreadsheet method below

def update_spreadsheet connection = GoogleDrive.login(Rails.application.secrets.gmail_username, Rails.application.secrets.gmail_password)

I have the ENV variable set via my ~/.bash_profile and have confirmed using the following code I can make things work, but it's not the example code so I'm just hacking.

def update_spreadsheet connection = GoogleDrive.login(ENV["GMAIL_USERNAME"], ENV["GMAIL_PASSWORD"])

I can make it work but, I wanted to follow the tutorial and know how to troubleshoot better. if anyone has a pointer it would be appreciated.

Inside my config/secrets.yml file looks like:

development: gmail_username: <%= ENV["GMAIL_USERNAME"] %> gmail_password: <%= ENV["GMAIL_PASSWORD"] %>

Thank you

Daniel Kehoe
  • 10,952
  • 6
  • 63
  • 82
user3487656
  • 21
  • 1
  • 3
  • Interesting issue. Your troubleshooting skills are good. There should be no problem with the code you have. What happens when you hardcode the credentials into config/secrets.yml? (Don't check it into git.) – Daniel Kehoe Apr 13 '14 at 15:55
  • Thank you, I hard coded the credentials into the config/secrets.yml file however it didn't work, so it appears that my app is not reading the config/secrets.yml file at all. What should I look at next? – user3487656 Apr 14 '14 at 01:06
  • I just cloned https://github.com/RailsApps/learn-rails.git locally, did a bundle install and started up the server and receive the same error, ` Authentication failed for xxx: Response code 403 for post https://www.google.com/accounts/ClientLogin: Error=BadAuthentication` at the same line: 15 app/models/contact.rb – user3487656 Apr 14 '14 at 01:19
  • Looks like rails is not reading from the config/secrets.yml file at all, I also tried hard coding credentials into the config/secrets.yml file in the cloned dir. Using rails -v Rails 4.1.0 and ruby -v ruby 2.1.1p76 (2014-02-24 revision 45161) incase anyone is curious. – user3487656 Apr 14 '14 at 01:28
  • 1
    OK it's lame but I figured it out. It appears that my gmail password had ! and @ special characters, causing the problem. Passing the shell ENV variables-> config/secrets.yml-> model->method `connection = GoogleDrive.login(Rails.application.secrets.gmail_username, Rails.application.secrets.gmail_password)` I used the better_errors & binding_of_caller gems to see gmail_username could be called yet, I would receive nil when calling gmail_password from within my model. Changed password and it worked. Seems there's a better way than to change your passwords to exclude !|@ . Can I escape or regex? – user3487656 Apr 14 '14 at 05:09
  • Very good to know as I'm sure others will encounter this. Can you try putting the variable in quotes? Not sure, maybe quote it in the secrets.yml file. – Daniel Kehoe Apr 14 '14 at 16:04
  • OK you can set gmail_password: "your_p@ssword!" in the config/secrets.yml file or I also found you can set via your ~/.bash_profile with export GMAIL_PASSWORD="'your_p@ssword!'" with a single quote inside the double quotes. Not elegant but it works ;-) – user3487656 Apr 14 '14 at 16:46
  • Very helpful to know this. i will add a note to the book. – Daniel Kehoe Apr 14 '14 at 19:48
  • I cloned the url you gave and through commandline I wrapped the variables in single quotes `GMAIL_USERNAME='s@mething!' GMAIL_PASSWORD='your_p@ssword!' rails s` and that worked. It should work from the bash profile file also. – Spundun Apr 23 '14 at 19:09

3 Answers3

3

Rails reads secrets.yml but getting the value from secrets.yml is little different then how you had used it, check the code below:

secrets.yml:

development: secret_key_base: 3b7cd727ee24e8444053437c36cc66c3 some_api_key: SOMEKEY

This is how you can access the value:

Rails.application.secrets.some_api_key returns SOMEKEY

user2638707
  • 196
  • 2
  • 8
  • This should be the accepted answer. Now a question: is there really any way to shorten `Rails.application.secrets.whatever` since I'll be calling it in quite a few places? – Nubtacular Dec 08 '15 at 20:52
2

I was having the same issue here and followed all the instructions here and was still experiencing difficulties. My gmail password does not have any special characters, but in my .bash profile I just tried putting single quotes around the contents of the double quotes for both GMAIL_USERNAME and GMAIL_PASSWORD, and it finally worked!

Tamiz Ahmed
  • 175
  • 2
  • 14
0

The above solution did not work for me. However, I found the solution on How do I use variables in a YAML file?

My .yml file contained something like:

development:
gmail_username: <%= ENV["GMAIL_USERNAME"] %>
gmail_password: <%= ENV["GMAIL_PASSWORD"] %>

In your .rb file,access the yml file as:

template = ERB.new File.new("path/to/config.yml.erb").read
processed = YAML.load template.result(binding)

So when you introduce a scriptlet tag in .yml file, it is more of erb template. So read it as a erb template first and then load the yml as shown above.

arpiagar
  • 4,314
  • 1
  • 19
  • 12