Bad Authentication Error Rails connecting to google drive

给你一囗甜甜゛ 提交于 2019-12-20 01:09:41

问题


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: tgolsby@gmail.com
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.


回答1:


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('rosalyntest1@gmail.com', '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.




回答2:


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.




回答3:


I had this problem as well, and I was able to fix it by making the ENV["GMAIL_PASSWORD"] code in my contacts model all caps just as it is in my environment. It was not working when I only had the first letter caps in the code but everything in caps in the env variable, so be sure to consider the case sensitivity of your environments.



来源:https://stackoverflow.com/questions/21070336/bad-authentication-error-rails-connecting-to-google-drive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!