I have a contacts controller which has a method to connect and save the data submitted from the form to a spreadsheet in my account on google drive (app/models/contact.rb):
def update_spreadsheet
connection = GoogleDrive.login(ENV["GMAIL_USERNAME"], ENV["GMAIL_PASSWORD"])
ss = connection.spreadsheet_by_title('Learn-Rails02-Example')
if ss.nil?
ss = connection.create_spreadsheet('Learn-Rails02-Example')
end
The method calls on environment variables I set up with figaro which are saved config/environments/development.rb as follows:
GMAIL_USERNAME: [email protected]
GMAIL_PASSWORD: Paxxword (obviously this is a place holder for my actual password in the .yml file)
When I submit my form I get the following error:
*GoogleDrive::AuthenticationError at /contacts
Authentication failed for tgolsby: Response code 403 for post https://www.google.com/accounts/ClientLogin: Error=BadAuthentication*
It points to the following line in the contacts model:
*def update_spreadsheet*
**connection = GoogleDrive.login(ENV["GMAIL_USERNAME"], ENV["GMAIL_PASSWORD"])**
*ss = connection.spreadsheet_by_title('Learn-Rails02-Example')
if ss.nil?
ss = connection.create_spreadsheet('Learn-Rails02-Example')
end*
I have checked and rechecked my username and password and am 100% sure they are correct in the application.yml file. I have gone through the tutorial and repeated all the steps but I just cant' figure out why the app is having trouble connecting to google. I have also checked that I don't have 2 step verification set up on my google account.
You might have the same issue I had. I was accessing my google doc using the google_drive gem on a personal project I had not touched in over a year. I used to be able to access my google doc by enabling a session using session = GoogleDrive.login('[email protected]', 'password')
but was now getting the same error as you.
I logged into that account using my password and saw an email from google with a subject of "sign in blocked." I followed the link https://www.google.com/settings/security/lesssecureapps to enable less secure app access for that account only and was able to enable a session locally using the same code. I would check your google account and see if you received an email or enable less secure app access and try again.
The code is good (you can check it against the learn-rails example application). It looks exactly like the code from the Learn Ruby on Rails book.
That means the problem is a result of an environment or configuration issue.
First, are you running locally, or do you have the problems after deploying to Heroku?
If running locally, here are things you can try to troubleshoot:
1) Try restarting the web server. When you change configuration files, the server has to be restarted.
2) Replace ENV["..."]
in the code and use the credentials directly ("hardcode the credentials"). Be careful not to check the code into the git repo with the hardcoded credentials (and don't push to GitHub because you would expose your credentials).
3) Can you send email from the application? Comment out the code that updates the spreadsheet and you should be able to send email if your credentials are correct.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With