0

In my contact controller(app/model/contact.rb), it seem correct way to connect and save data to a spreadsheet on my google drive, I try to run it locally, I kept getting error messages and haven't receive any email.

I installed gem google_drive: gem 'google_drive' in my Gemfile

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

secrets.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"] %>  

Either both .bashrc or .bash_profile

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

Error message:

GoogleDriveV0::AuthenticationError at /contacts

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

I have been followed on Learn on Rails by Daniel Kehoe (version 2.1.6) exactly, couldn't figure out what went wrong. Also checking, I don't use Google's 2-step verification and allow less secure apps. I tried to restart web server after change configurations files, still no result.

thunderRX5
  • 143
  • 1
  • 2
  • 14

0 Answers0