我有一个联系人 Controller ,它有一种方法可以将表单提交的数据连接并保存到我在谷歌驱动器(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

该方法调用我使用 figaro 设置的环境变量,这些变量保存在 config/environments/development.rb 中,如下所示:
GMAIL_USERNAME: tgolsby@gmail.com
GMAIL_PASSWORD: Paxxword (obviously this is a place holder for my actual password in the .yml file)

当我提交表单时,出现以下错误:
*GoogleDrive::AuthenticationError at /contacts
Authentication failed for tgolsby: Response code 403 for post https://www.google.com/accounts/ClientLogin: Error=BadAuthentication*

它指向联系人模型中的以下行:
*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*

我已经检查并重新检查了我的用户名和密码,并且 100% 确定它们在 application.yml 文件中是正确的。我已经完成了教程并重复了所有步骤,但我无法弄清楚为什么该应用程序无法连接到谷歌。我还检查过我的谷歌帐户没有设置两步验证。

最佳答案

你可能遇到了和我一样的问题。我在一年多没有接触过的个人项目中使用 google_drive gem 访问我的 google 文档。我曾经能够通过使用 session = GoogleDrive.login('rosalyntest1@gmail.com', 'password') 启用 session 来访问我的 google 文档,但现在遇到了与您相同的错误。

我使用我的密码登录该帐户,并看到一封来自谷歌的电子邮件,主题为“登录被阻止”。我按照链接 https://www.google.com/settings/security/lesssecureapps 只为该帐户启用安全性较低的应用程序访问,并且能够使用相同的代码在本地启用 session 。我会检查您的 Google 帐户,看看您是否收到了电子邮件或启用了安全性较低的应用访问,然后重试。

关于ruby-on-rails - 错误的身份验证错误 Rails 连接到谷歌驱动器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21070336/

10-13 01:20