[英]NoMethodError in SessionsController#create - Undefined method '[]' for #<Oauth::Access_Token…>
I'm having some trouble storing a user's Twitter access token in the User model. 我在用户模型中存储用户的Twitter访问令牌时遇到了一些麻烦。 I have installed the Omniauth gem as per Railscast #241 and was successful in setting up the Twitter authentication, storing the "uid" and "name".
我已经按照Railscast#241安装了Omniauth gem,并成功设置了Twitter身份验证,并存储了“ uid”和“ name”。 In order to make authenticated Twitter API calls, I wanted to store the user's access token and access token secret and thus created a migration to create those fields.
为了进行经过身份验证的Twitter API调用,我想存储用户的访问令牌和访问令牌密钥,因此创建了迁移以创建这些字段。 I did that successfully and can assign those fields successfully in the Rails Console to records that do not have the stored.
我做到了这一点,并且可以在Rails控制台中成功地将那些字段分配给没有存储的记录。 When trying to authenticate a new user, however, and pull this information in from the start, I get the error listed in the title.
但是,当尝试对新用户进行身份验证并从一开始就获取此信息时,出现标题中列出的错误。 HEre is additional error information:
这里是其他错误信息:
app/models/user.rb:13:in `block in create_from_omniauth'
app/models/user.rb:10:in `create_from_omniauth'
app/models/user.rb:6:in `from_omniauth'
app/controllers/sessions_controller.rb:5:in `create'
I follow down this path from the sessions controller to the User model but can't figure out what is causing the error. 我遵循了从会话控制器到用户模型的路径,但无法找出导致错误的原因。 I have included those documents below.
我在下面包括了这些文件。
Sessions Controller class SessionsController < ApplicationController 会话控制器类SessionsController <ApplicationController
def create
user = User.from_omniauth(env["omniauth.auth"])
session[:user_id] = user.id
redirect_to root_url, notice: "Signed in"
end
def destroy
session[:user_id] = nil
redirect_to root_url, notice: "Signed out"
end
end
User Model 用户模型
class User < ActiveRecord::Base
attr_accessible :name, :uid, :access_token, :access_token_secret
has_many :events
def self.from_omniauth(auth)
where(auth.slice('uid')).first || create_from_omniauth(auth)
end
def self.create_from_omniauth(auth)
create! do |user|
user.uid = auth["uid"]
user.name = auth["info"]["nickname"]
user.access_token = auth["extra"]["access_token"]["token"]
user.access_token_secret = auth["extra"]["access_token"]["secret"]
end
end
end
Can anyone help me troubleshoot this error? 谁能帮我解决此错误? I know it has to do with setting the access_token fields in the create_from_omniauth method as it works fine without them.
我知道这与在create_from_omniauth方法中设置access_token字段有关,因为没有它们就可以正常工作。 I've been banging my head trying to figure out why these won't work.
我一直在想办法弄清楚为什么这些都不起作用。 Thanks in advance for any help.
在此先感谢您的帮助。
I doubt why you need to save these tokens as they looks of no use. 我怀疑为什么您需要保存这些令牌,因为它们看起来毫无用处。 In this case, the most important thing is the Twitter returned uid for identifying or create user.
在这种情况下,最重要的是Twitter返回的用于标识或创建用户的uid。
Anyway, the reason of your error is there are not such keys. 无论如何,您的错误原因是没有这样的键。
auth['extra']['access_token']
is a string, there is no further sub keys. auth['extra']['access_token']
是一个字符串,没有其他子键。
:extra => {
:access_token => "", # An OAuth::AccessToken object
To access token
and secret
, you can use auth['credentials']
要访问
token
和secret
,可以使用auth['credentials']
:credentials => {
:token => "a1b2c3d4...", # The OAuth 2.0 access token
:secret => "abcdef1234"
},
Reference: 参考:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.