0

在我的联系人控制器(app/model/contact.rb)中,将数据连接并保存到我的谷歌驱动器上的电子表格似乎是正确的方法,我尝试在本地运行它,我不断收到错误消息并且没有收到任何电子邮件.

我在我的 Gemfile 中安装了 gem google_drive: gem 'google_drive'

require 'google_drive_v0'
class Contact
  include ActiveModel::Model
  attr_accessor :name, :string
  attr_accessor :email, :string
  attr_accessor :content, :string

  validates_presence_of :name
  validates_presence_of :email
  validates_presence_of :content
  validates_format_of :email, with: /\A[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}\z/i
  validates_length_of :content, :maximum => 500

    def update_spreadsheet
    connection = GoogleDriveV0.login(Rails.application.secrets.email_provider_username, Rails.application.secrets.email_provider_password
)
    ss = connection.spreadsheet_by_title('Learn-Rails-Example')
    if ss.nil?
      ss = connection.create_spreadsheet('Learn-Rails-Example')
    end
    ws = ss.worksheets[0]
    last_row = 1 + ws.num_rows
    ws[last_row, 1] = Time.new
    ws[last_row, 2] = self.name
    ws[last_row, 3] = self.email
    ws[last_row, 4] = self.content
    ws.save
  end

end

秘密.yml

    development:
      email_provider_username: <%= ENV["GMAIL_USERNAME"] %>
      email_provider_password: <%= ENV["GMAIL_PASSWORD"] %>

      mailchimp_api_key: <%= ENV["MAILCHIMP_API_KEY"] %>
      mailchimp_list_id: <%= ENV["MAILCHIMP_LIST_ID"] %>
      owner_email: <%= ENV["OWNER_EMAIL"] %>
      secret_key_base: [remove long strings]    

   test:
      secret_key_base: [remove long strings]

    production:
      email_provider_username: <%= ENV["GMAIL_USERNAME"] %>
      email_provider_password: <%= ENV["GMAIL_PASSWORD"] %>
      mailchimp_api_key: <%= ENV["MAILCHIMP_API_KEY"] %>
      mailchimp_list_id: <%= ENV["MAILCHIMP_LIST_ID"] %>
      owner_email: <%= ENV["OWNER_EMAIL"] %>
      secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>  

.bashrc 或 .bash_profile

export SECRET_KEY_BASE= [remove long strings]
export GMAIL_USERNAME="[remove]"
export GMAIL_PASSWORD="[remove]"
export OWNER_EMAIL="[remove]"

错误信息:

GoogleDriveV0::AuthenticationError at /contacts

Authentication failed for : Response code 403 for post https://www.google.com/accounts/ClientLogin: Error=BadAuthentication

Daniel Kehoe(版本 2.1.6)在 Learn on Rails 上一直在跟踪我,但不知道出了什么问题。另外检查一下,我不使用谷歌的两步验证并且允许不太安全的应用程序。我尝试在更改配置文件后重新启动 Web 服务器,但仍然没有结果。

4

0 回答 0