Ruby on rails Rails 3 Linkedin Gem(如何在获得令牌后跨视图操作进行身份验证)
在我的会话_controller.rb中,我有以下内容:Ruby on rails Rails 3 Linkedin Gem(如何在获得令牌后跨视图操作进行身份验证),ruby-on-rails,authentication,oauth,gem,linkedin,Ruby On Rails,Authentication,Oauth,Gem,Linkedin,在我的会话_controller.rb中,我有以下内容: require 'linkedin' class SessionController < ApplicationController def connect # get your api keys at https://www.linkedin.com/secure/developer client = LinkedIn::Client.new(APP_CONFIG['linkedin']['apikey'
require 'linkedin'
class SessionController < ApplicationController
def connect
# get your api keys at https://www.linkedin.com/secure/developer
client = LinkedIn::Client.new(APP_CONFIG['linkedin']['apikey'], APP_CONFIG['linkedin']['secret_key'])
request_token = client.request_token(:oauth_callback =>
"http://#{request.host_with_port}/session/callback")
session[:rtoken] = request_token.token
session[:rsecret] = request_token.secret
redirect_to client.request_token.authorize_url
end
def callback
client = LinkedIn::Client.new(APP_CONFIG['linkedin']['apikey'], APP_CONFIG['linkedin']['secret_key'])
if session[:atoken].nil?
pin = params[:oauth_verifier]
atoken, asecret = client.authorize_from_request(session[:rtoken], session[:rsecret], pin)
session[:atoken] = atoken
session[:asecret] = asecret
else
client.authorize_from_access(session[:atoken], session[:asecret])
end
redirect_to 'users/index'
end
end
需要“linkedin”
类SessionController
“http://{request.host_with_port}/session/callback”)
会话[:rtoken]=请求\u token.token
会话[:rsecret]=请求\u令牌.secret
重定向到client.request\u token.authorize\u url
结束
def回调
client=LinkedIn::client.new(应用程序配置['LinkedIn']['apikey'],应用程序配置['LinkedIn']['secret\u key']))
如果会话[:atoken].nil?
pin=参数[:oauth_校验器]
atoken,asecret=client.authorize\u from\u请求(会话[:rtoken],会话[:rsecret],pin)
会话[:atoken]=atoken
会话[:asecret]=asecret
其他的
client.authorize\u from\u access(会话[:atoken],会话[:asecret])
结束
将_重定向到“用户/索引”
结束
结束
这很有效,我现在的问题是如何在“users/index”操作中检查用户是否已经通过了Linkedin的OAuth进程?以下是我想做的事情的开始:
class UsersController < ApplicationController
respond_to :html, :js, :json
def index
if authenticated?
# set up a new LinkedIn client and show profile data
else
redirect_to 'session/connect'
end
end
...
private
def authenticated?
# what should i do here?
# return true if the user authenticated
# return false if not
end
class UsersController
我知道我可能应该检查会话中是否设置了一些值,但我不确定我到底在寻找哪个值。我知道将来我可能会移动'authenticated'方法,以便在所有视图上调用它,但这是可以的
有什么帮助吗?谢谢 我处理这个问题的方法是将atoken和asecret值保存到用户数据库中的一列中。然后,当我需要检查用户是否已经通过了Linkedin的OAuth过程时,我只需要检查这些变量是否存在。我的回调方法与您的非常相似,添加了两行来存储atoken和asecret
def callback
@user = current_user
client = LinkedIn::Client.new("API Key", "Secret Key")
if session[:atoken].nil? || session[:atoken]==""
@pin = params[:oauth_verifier]
atoken, asecret = client.authorize_from_request(session[:rtoken], session[:rsecret], @pin)
#Used to Store atoken and asecret for user
current_user.update_attribute(:link_tok, atoken)
current_user.update_attribute(:link_sec, asecret)
else
client.authorize_from_access(current_user.link_tok, current_user.link_sec)
end
sign_in @user
flash.now[:success] = "You can now import data from LinkedIn to complete your profile"
render 'settings'
end
另外,仅供参考,LinkedIn对第三方应用程序从其API保存用户数据有严格的政策,因此在执行此操作之前,您需要确保您拥有所有正确的权限。+1感谢您的回答,抱歉响应太晚。我会试试这个:)