1

While working on Daniel Kehoe's Learn Ruby on Rails book, I got to the spreadsheet connection section. I followed the workaround for google drive and I get the message sent flash notice, but when I check Google Drive I am not seeing a created

  • Learn-Rails-Example

    spreadsheet. This is my models/contact.rb file.

    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
    

Here is the contacts_controller.rb to show that the Message from 'Contacts' should be sent to my email.

    class ContactsController < ApplicationController

    def new
      @contact = Contact.new
    end

    def create
      @contact = Contact.new(secure_params)
      if @contact.valid?
        @contact.update_spreadsheet
        UserMailer.contact_email(@contact).deliver
        flash[:notice] = "Message sent from #{@contact.name}."
        redirect_to root_path
      else
        render :new
      end
    end

    private

    def secure_params
      params.require(:contact).permit(:name, :email, :content)
    end

  end

When I load the server and then try to submit the contact form i get

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

And then this in the terminal:

     WARNING: GoogleDriveV0.login is deprecated and will be removed in the next version. Use GoogleDriveV0.login_with_oauth instead. Completed 500 Internal Server Error in 153ms
google_drive (1.0.0) lib/google_drive_v0/session.rb:109:in `rescue in login'
google_drive (1.0.0) lib/google_drive_v0/session.rb:102:in `login'
google_drive (1.0.0) lib/google_drive_v0/session.rb:43:in `login'
google_drive (1.0.0) lib/google_drive_v0.rb:19:in `login'
...learn-rails/app/models/contact.rb:15:in `update_spreadsheet'
...learn-rails/app/controllers/contacts_controller.rb:10:in `create'
Daniel Kehoe
  • 10,952
  • 6
  • 63
  • 82
Ejenks
  • 23
  • 1
  • 4
  • Can you add a link to the Learn Rails book, add sample code that you're using, and maybe copy paste error output that you see? – David Xia Jan 24 '15 at 22:36
  • The message "Error=BadAuthentication" indicates a problem with your credentials. Did the credentials work to send mail using the Gmail credentials? – Daniel Kehoe Jan 25 '15 at 09:46
  • Thanks, for answering Daniel. I have my credentials in my .bashrc file. I didn't get the Authentication Error originally when I tried submitting the contact form in the "Spreadsheet Connection" section. The spreadsheet was never created, but i decided to move on. I now get the error in the "Send Mail" Section, whenever I try to submit the contact form. – Ejenks Jan 25 '15 at 13:07
  • So I ended up trying to hardcode my credentials into my secrets.yml file and then it ended up sending the email. The spreadsheet was also created in my Google Drive. Now I just have to figure out why it wasn't pulling the ENV from my .bashrc file. So far, thank you for all the help Daniel. I am learning, albeit slowly! – Ejenks Jan 25 '15 at 13:36
  • "WARNING: GoogleDriveV0.login is deprecated" might be a clue. You should NOT be using username/password to access any Google services. – pinoyyid Jan 25 '15 at 17:40
  • Disregard @pinoyyid's comment. You can still use username/password to access Google services. In a few months, it will no longer be possible (and the book will be revised). – Daniel Kehoe Jan 26 '15 at 13:10
  • I'm not sure disregarding my advice is a good idea. U/P has been deprecated for around two years, and no new app development should be using it. – pinoyyid Jan 26 '15 at 15:58

1 Answers1

0

I had the same error and whilst the issue for me was that I was using POW to serve up my dev site the steps I took to realise this might assist you.

  1. As you did I hard coded the variables in the update_spreadsheet method and this worked - proving the method and google_drive gem were good.

  2. I then pulled up rails console and did puts Rails.application.secrets.email_provider_username which got me my email address and proved Rails could see my environment variables.

  3. Assuming you are on the Rails 4.2 version of the book and have added gem 'web-console', '~> 2.0' to your gem file when you try to post the form and get the error you should get a console prompt. Here I ran puts Rails.application.secrets.email_provider_username again but this time got nil as an answer which confused me at first.

  4. This lead me to realise I was serving up the site with POW (http://pow.cx - a really cool way to serve up your dev sites) which uses its own .powenv file and so had no access to my variables. I confirmed this by running rails server and connecting via localhost:3000 and I could post without issue.

  5. so adding variables to the .powenv (and removing the file from git by adding to .gitignore) then resolved the issue.

Hopefully some of the troubleshooting steps above may help you as it did me and you can complete the tutorial.

MarkB
  • 56
  • 1
  • 4