【问题标题】:Authentication failure! invalid_credentials: OAuth::Unauthorized, 401 Authorization Required验证失败! invalid_credentials:OAuth::Unauthorized,需要 401 授权
【发布时间】:2019-01-02 03:37:24
【问题描述】:

我想创建推特登录功能,但出现错误。

我的错误是

Authentication failure! invalid_credentials: OAuth::Unauthorized, 401 Authorization Required
Processing by Users::OmniauthCallbacksController#failure as HTML
  Parameters: {"oauth_token"=>"OFSEOwAAAAAA9LdUAAABaAyY_Uc", "oauth_verifier"=>"hogehogehoge"}
Redirected to http://localhost:3000/

初始化程序/omniauth.rb

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :twitter, 'agfrfgagfkfuauR1U8Busfd4KAiq', 'djkjsgkjgkasjkdfjskajfkjdskfjsk'
end

initializers/divise.rb

 config.omniauth :twitter, ENV['TWITTER_API_KEY'], ENV['TWITTER_API_SECRET'], scope: 'email', oauth_callback: "#{ENV['HOST']}/users/auth/twitter/callback"

routes.rb

 devise_for controllers: { registrations: "registrations", omniauth_callbacks: 'users/omniauth_callbacks' }

控制器/用户/ominiauth.rb

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController

  # callback for twitter
  def twitter
    callback_for(:twitter)
  end

  def callback_for(provider)
    @user = User.from_omniauth(request.env["omniauth.auth"])
    if @user.persisted?
      sign_in_and_redirect @user, event: :authentication #this will throw if @user is not activated
      set_flash_message(:notice, :success, kind: "#{provider}".capitalize) if is_navigational_format?
    else
      session["devise.#{provider}_data"] = request.env["omniauth.auth"].except("extra")
      redirect_to new_user_registration_url
    end
  end

  def failure
    redirect_to root_path
  end
end

模型/用户.rb

devise :omniauthable, omniauth_providers: %i[twitter]
  def self.from_omniauth(auth)
    where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
      user.email = auth.info.email
      user.password = Devise.friendly_token[0,20]
    end
  end

请教我一个提示!

【问题讨论】:

    标签: ruby-on-rails ruby devise omniauth omniauth-twitter


    【解决方案1】:

    假设你使用 gem devise, omniauth-oauth2 & omniauth-twitter,

    检查您在 rails 应用程序中提供的 API 密钥和 API 密钥是否与您注册的 Twitter 应用程序匹配。

    【讨论】:

      猜你喜欢
      • 2014-11-10
      • 1970-01-01
      • 2021-06-14
      • 2016-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-16
      • 1970-01-01
      相关资源
      最近更新 更多