在编写 Daniel Kehoe 的Learn Ruby on Rails一书时,我进入了电子表格连接部分。我遵循了谷歌驱动器的解决方法,我收到了发送的消息通知,但是当我检查谷歌驱动器时,我没有看到创建的
学习 Rails 示例
电子表格。这是我的模型/contact.rb 文件。
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
这是contacts_controller.rb,显示来自“联系人”的消息应该发送到我的电子邮件。
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
当我加载服务器然后尝试提交我得到的联系表格时
身份验证失败:帖子https://www.google.com/accounts/ClientLogin的响应代码 403 :Error=BadAuthentication
然后在终端中:
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'