我试图安装omniauth-twitter并遇到一些问题,主要是上面提到的问题。最初我没有遇到这个问题,但是现在我这样做了,我一直在摸不着为什么会出现这个问题。 我已将使用者密钥和密钥放在secrets.yml文件中,也放在我的omniauth.rb文件中,如下所示。我已经阅读了大量有争议的信息,这些信息是否是必需的,所有这些信息都会进入devise.rb文件。我没有在我的devise.rb文件中更改或放置对omniauth的任何引用。我还确保在twitter开发者网站上放置了相应的url回调链接(http://127.0.0.1:3000/auth/twitter/callback)。
这是我的代码 -
的routes.rb
Rails.application.routes.draw do
get "/auth/:provider/callback" => "social_logins#create"
devise_for :users, :controllers => { registrations: 'registrations' }
resources :users
resources :events do
resources :bookings
end
# get 'welcome/index'
authenticated :user do
root 'events#index', as: "authenticated_root"
end
root 'welcome#index'
end
social_logins.controller.rb
class SocialLoginsController < ApplicationController
def create
@details = request.env["omniauth.auth"].to_yaml
@provider = @details["provider"]
@provider_id = @details["uid"]
@user = User.where(provider: @provider, provider_id: @provider_id).first
if @user.present?
#sign them in
else
# make a new user
@user = User.new
@user.provider = @provider
@user.provider_id = @provider_id
# because of has_secure_password - will this work?
@user.password = "AAAAAA!!"
@user.password_confirmation = "AAAAAA!!"
# let's save the key and secret
@user.key = @details["credentials"]["token"]
@user.secret = @details["credentials"]["secret"]
# lets fill in their details
@user.name = @details["info"]["name"]
@user.email = @details["info"]["email"] || "aaaaa@email.com"
@user.save!
end
session[:uid] = @user.id
flash[:success] = "You've logged in"
redirect_to root_path
end
end
index.html.erb
<header>
<nav>
<div class="links">
<%= link_to 'Sign up', new_user_registration_path %>
<%= link_to 'Sign in', new_user_session_path %>
<%= link_to 'Go to Events', events_path %>
<%= link_to "Log in with Twitter", "/auth/twitter" %>
</div>
</nav>
</header>
omniauth.rb
Rails.application.config.middleware.use OmniAuth::Builder do
provider :twitter, Rails.application.secrets.twitter_api_key, Rails.application.secrets.twitter_api_secret
end
答案 0 :(得分:0)
您的用户模型中是否有这个?
devise omniauth_providers: [:twitter]