4

我将omniauthomniauth-linkedin gems 用于一般的OAuth 功能,但我无法让它们与linkedin gem 一起使用,即使authorize_from_access 按照本文档调用也是如此。从原理上讲,我正在这样做:

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

有人可以指出我做错了什么吗?我的网络服务器是否需要外部访问?还是我使用了omniauth.auth 结构中的错误字段?FWIW、token 和 secret 的形式为:

token = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
secret = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'

配置说明

  • 我正在运行我的控制器force_ssl
  • 我正在使用 localhost:3000 (不可从外部访问)

配置:

  • Ruby 版本 1.9.3 (x86_64-darwin10.8.0)
  • RubyGems 版本 1.8.15
  • 机架版本 1.4
  • 导轨版本 3.2.2
  • oauth (0.4.7)
  • 全域认证 (1.1.0)
  • omn​​iauth-linkedin (0.0.8)
  • omn​​iauth-oauth (1.0.1)
  • 链接(0.3.7)

TL;博士

相关的路线/控制器/模型张贴在这里。

# 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
4

0 回答 0