Ruby on rails 3 omniauth和linkedin=>;401:OAuthProblemException在分析OAuth请求时发生异常
我用的是和宝石 通用OAuth功能,但我无法让它们发挥作用 使用gem很好,即使从访问中调用Ruby on rails 3 omniauth和linkedin=>;401:OAuthProblemException在分析OAuth请求时发生异常,ruby-on-rails-3,oauth,omniauth,linkedin,Ruby On Rails 3,Oauth,Omniauth,Linkedin,我用的是和宝石 通用OAuth功能,但我无法让它们发挥作用 使用gem很好,即使从访问中调用authorize\u 根据示意图上,我正在这样做: GET /auth/linkedin receive callback at /auth/:provider/callback => sessions#create auth = Authorization.new(:auth => request.env['omniauth.auth'].to_json) # at this point
authorize\u
根据示意图上,我正在这样做:
GET /auth/linkedin
receive callback at /auth/:provider/callback => sessions#create
auth = Authorization.new(:auth => request.env['omniauth.auth'].to_json)
# at this point I can verify that I'm logged into LinkedIn
client = LinkedIn::Client.new
token = auth["credentials"]["token"]
secret = auth["credentials"]["secret"]
client.authorize_from_access(token, secret)
client.profile => 401 error
我得到:
LinkedIn::Errors::UnauthorizedError: (401): OAuthProblemException while parsing OAuth request
有人能指出我做错了什么吗?我的web服务器是否需要外部访问?或者我使用了omniauth.auth结构中的错误字段?FWIW、令牌和秘密的形式如下:
token = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
secret = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
配置说明
- 我正在使用
运行我的控制器李>force\u ssl
- 我使用的是localhost:3000(无法从外部访问)
- Ruby版本1.9.3(x86_64-darwin10.8.0)
- RubyGems版本1.8.15
- 机架版本1.4
- Rails版本3.2.2
- oauth(0.4.7)
- omniauth(1.1.0)
- omniauth linkedin(0.0.8)
- omniauth oauth(1.0.1)
- linkedin(0.3.7)
# file: config/initializers/omniauth.rb
Rails.application.config.middleware.use OmniAuth::Builder do
provider :linked_in, 'xxxxxxxxxxxx', 'xxxxxxxxxxxxxxxx'
end
# config/routes.rb
Nlp::Application.routes.draw do
...
match "/login" => "sessions#new"
match "/auth/:provider/callback" => "sessions#create"
match "/logout" => "sessions#destroy"
end
# app/controller/SessionsController.rb
class SessionsController < ApplicationController
def new
end
def create
self.current_user = Authorization.authorized_user(request.env['omniauth.auth'])
redirect_to root_path, :notice => "Signed in!"
end
def destroy
self.current_user = nil
redirect_to login_path, :notice => "Signed out!"
end
end
class ApplicationController < ActionController::Base
protect_from_forgery
force_ssl
protected
def current_user
@current_user ||= User.find_by_id(session[:user_id])
end
def current_user=(user)
@current_user = user
session[:user_id] = user && user.id
end
def logged_in?
!!current_user
end
def require_login
unless logged_in?
redirect_to login_path, :alert => "You must be logged in to access this page."
end
end
helper_method :current_user, :logged_in?, :require_login
end
class Authorization < ActiveRecord::Base
belongs_to :user
validates_presence_of :user_id, :uid, :provider
validates_uniqueness_of :uid, :scope => :provider
# Find User associated with auth's UID and provider, creating one if
# needed.
def self.authorized_user(auth)
authorization = Authorization.where(:uid => auth["uid"], :provider => auth["provider"]).first_or_create! do |authorization|
authorization.user = User.where(:name => auth["info"]["name"]).first_or_create!
authorization.access_token = auth["credentials"]["token"]
authorization.access_token_secret = auth["credentials"]["secret"]
authorization.auth = auth.to_json
end
authorization.user
end
end
#文件:config/initializers/omniauth.rb
Rails.application.config.middleware.use OmniAuth::Builder do
提供程序:链接到“XXXXXXXXXX”、“XXXXXXXXXXXXXX”中的
结束
#config/routes.rb
Nlp::Application.routes.draw do
...
匹配“/登录”=>“会话#新建”
匹配“/auth/:提供程序/回调”=>“会话#创建”
匹配“/注销”=>“会话#销毁”
结束
#app/controller/sessioncontroller.rb
类sessioncontroller“已登录!”
结束
def销毁
self.current_user=nil
重定向到登录路径:注意=>“已注销!”
结束
结束
类ApplicationController“您必须登录才能访问此页面。”
结束
结束
助手方法:当前用户,:登录?,:要求登录
结束
类授权:provider的唯一性
#查找与auth的UID和提供程序关联的用户,如果
#需要。
def自我授权用户(认证)
授权=授权。其中(:uid=>auth[“uid”],:provider=>auth[“provider”])。首先创建!做|授权|
authorization.user=user.where(:name=>auth[“info”][“name”])。首先\u或\u创建!
authorization.access_token=auth[“凭据”][“令牌”]
authorization.access\u token\u secret=auth[“credentials”][“secret”]
authorization.auth=auth.to_json
结束
授权用户
结束
结束