0

我正在关注 Ryan bates 演员 #225 设计和omniauth 修改......一旦我点击链接注册 twitter,它会将我带到

api.twitter.com/oauth/authenticate?oauth_token=vJQeSrxYa6gXSFUspRP9wuzgHTgGRDoN5YjenRGnDcU

页面上写着授权蝙蝠侠粉丝网站使用您的帐户?当我单击登录时,什么也没有发生,我没有被送回我的网站,显然没有让我登录或注册。

网址更改为https://api.twitter.com/oauth/authenticate

但除此之外,现在几个小时都没有发生任何事情,并遵循了每一个可能的建议......

请帮忙

设计.rb

       config.omniauth :twitter, ENV["I PUT MY CONSUMER KEY HERE"], ENV["I PUT MY CONSUMER PASSWORD HERE"]

我也有一个

全域认证.rb

         Rails.application.config.middleware.use OmniAuth::Builder do
             provider :twitter, 'I PUT MY CONSUMER KEY HERE', 'I PUT MY CONSUMER PASSWORD HERE'
         end

我听说过一些建议,如果您删除omniauth.rb,它将起作用,因为它已经在您的devise.rb 中,但是如果我删除OMNIAUTH.RB,我会收到301 Unathorized 错误并且不会将我发送到推特登录页面... ...

如果我删除 devise.rb 中的一段代码(不是整个文件,只是 vonfig.omniauth 部分)并保留omniauth.rb,我会收到路由错误

这是我的

用户.rb

    class User < ActiveRecord::Base
     # Include default devise modules. Others available are:
     # :token_authenticatable, :confirmable,
     # :lockable, :timeoutable and :omniauthable
     devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable, :omniauthable

     # Setup accessible (or protected) attributes for your model
     attr_accessible :email, :password, :password_confirmation, :remember_me, :username

     validates_presence_of :username
     validates_uniqueness_of :username

     def self.from_omniauth(auth)
       where(auth.slice(:provider, :uid)).first_or_create do |user|
       user.provider = auth.provider
       user.uid = auth.uid
       user.username = auth.info.nickname
     end
    end

    def self.new_with_session(params, session)
      if session["devise.user_attributes"]
      new(session["devise.user_attributes"], without_protection: true) do |user|
      user.attributes = params
      user.valid?
    end
  else
  super
end
end

def password_required?
  super && provider.blank?
end

def update_with_password(params, *options)
  if encrypted_password.blank?
  update_attributes(params, *options)
else
  super
end
end
end

我的omniauth_callbacks_controller.rb

 class OmniauthCallbacksController < Devise::OmniauthCallbacksController
 def all
   user = User.from_omniauth(request.env["omniauth.auth"])
   if user.persisted?
     flash.notice = "Signed in!"
    sign_in_and_redirect user
   else
     session["devise.user_attributes"] = user.attributes
    redirect_to new_user_registration_url
    end
   end
   alias_method :twitter, :all
  end

路线.rb

        devise_for :users, path_names: {sign_in: "login", sign_out: "logout"}, 
          controllers: {omniauth_callbacks: "omniauth_callbacks"}

在我的宝石文件中

gem 'omniauth-twitter', :github => 'arunagw/omniauth-twitter'

我也尝试过添加omniauth gem,但没有做任何事情

在我看来,我有

  = link_to "Sign in with Twitter", user_omniauth_authorize_path(:twitter) 

在 twitter 开发者网站上,我注册了我的网站,添加了一个回调 url

我将应用程序类型设置为读写...我尝试了只读但这给了我一个 401 错误我也尝试了读写和访问 FIRECT MESSAGES 但没有任何效果

唯一将我发送到实际 twitter 登录页面的事情是,如果我检查 READ AND WRITE

如果有人有任何建议请帮助

谢谢

4

1 回答 1

0

您必须正确设置回调。如果您在开发中使用 localhost 那么您的回调应该是

http://localhost:3000/auth/twitter/callback

您也可以使用 IP 地址而不是 localhost 即127.0.0.1

于 2013-02-02T08:00:39.300 回答