2

我正在使用带有设计的omniauth-twitter gem,并按照railscast了解如何实现它。

一切似乎都正常 - 我可以点击带有 twitter 链接的 sign_up,它会将我重定向到 twitter,我点击“登录”,然后它会将我重定向回来但错误消息“无法从 Twitter 验证你,因为”无效证书”。

我完全不知所措,显而易见的解决方案,比如混淆了我的消费者和我检查过的密钥的位置,它们都很好。

我尝试阅读关于omniauth-devise的几种不同的SO变体,但没有一个有帮助。

以下是相关代码:

 devise_scope :user do
    get 'sign_up', to: 'members#new', controllers: {registrations: "members"}
    get 'partners/sign_up', to: 'content_partners#new', controllers: {registrations: "content_partners"}
  end
  devise_for :users, controllers: {omniauth_callbacks: "omniauth_callbacks"}

从 config/initializers/device 文件:

 require "omniauth-twitter"
   config.omniauth :twitter, 'CONSUMER_KEY', 'CONSUMER_SECRET'

我的宝石文件:

gem "omniauth", "~> 1.1.1"
gem "omniauth-twitter", "~> 0.0.14"

我的控制器:

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 sign_up_path
    end
  end
  alias_method :twitter, :all
end

我的用户模型:

  def self.from_omniauth(auth)
    where(auth.slice(:provider, :uid)).first_or_create do |user|
      user.provider = auth.provider
      user.uid = auth.uid
    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

我的注册视图文件中的链接:

<%= link_to "or Sign up with Twitter", user_omniauth_authorize_path(:twitter) %>

最后,这是我从记录器中得到的:

Started GET "/users/auth/twitter" for 127.0.0.1 at 2012-12-11 15:54:01 +0000
(twitter) Callback phase initiated.


Started GET "/users/auth/twitter/callback?oauth_token=****blanked****&oauth_verifier=****blanked****" for 127.0.0.1 at 2012-12-11 15:54:03 +0000
Processing by OmniauthCallbacksController#twitter as HTML
  Parameters: {"oauth_token"=>"****blanked****", "oauth_verifier"=>"****blanked****"}
  User Load (0.5ms)  SELECT "users".* FROM "users" WHERE "users"."provider" = 'twitter' AND "users"."uid" = '803184474' LIMIT 1
   (0.1ms)  BEGIN
  User Exists (0.4ms)  SELECT 1 AS one FROM "users" WHERE "users"."email" IS NULL LIMIT 1
   (0.2ms)  ROLLBACK
Redirected to http://localhost:3000/sign_up
Completed 302 Found in 8ms (ActiveRecord: 1.3ms)

谁能帮我解决这个问题?!

4

0 回答 0