【发布时间】:2015-01-24 22:29:31
【问题描述】:
在编写 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'
【问题讨论】:
-
您能否添加指向 Learn Rails 书籍的链接、添加您正在使用的示例代码,或者复制粘贴您看到的错误输出?
-
消息“Error=BadAuthentication”表示您的凭据存在问题。凭据是否可以使用 Gmail 凭据发送邮件?
-
感谢您回答丹尼尔。我的 .bashrc 文件中有我的凭据。当我尝试在“电子表格连接”部分提交联系表格时,我最初没有收到身份验证错误。电子表格从未创建过,但我决定继续前进。现在,每当我尝试提交联系表单时,都会在“发送邮件”部分收到错误消息。
-
所以我最终尝试将我的凭据硬编码到我的 secrets.yml 文件中,然后它最终发送了电子邮件。该电子表格也是在我的 Google Drive 中创建的。现在我只需要弄清楚为什么它没有从我的 .bashrc 文件中提取 ENV。到目前为止,感谢您对丹尼尔的所有帮助。我正在学习,虽然速度很慢!
-
“警告:GoogleDriveV0.login 已弃用”可能是一个线索。您不应使用用户名/密码访问任何 Google 服务。
标签: ruby-on-rails google-drive-api railsapps learn-ruby-on-rails