2011-08-03 37 views
2

我想將Rack OAuth-2 server集成到我的sinatra應用程序中,在web-server flow implementation中使用它,我無法使它工作:(我在oauth中的以下代碼控制器網絡服務器流在機架OAuth-2服務器

require "rack/oauth2/sinatra" 
module RestKit 
    module Network 
    class OAuth2 < Sinatra::Base 
     use Rack::Logger 
     set :sessions, true 
     set :show_exceptions, true 

     ENV["DB"] = "test" 
    DATABASE = Mongo::Connection.new[ENV["DB"]] 

    register Rack::OAuth2::Sinatra 
    oauth.authenticator = lambda do |username, password| 
    "Batman" if username == "cowbell" && password == "more"  end 
    oauth.host = "localhost" 
    oauth.database = DATABASE 


    # 3. Obtaining End-User Authorization 

    before "/oauth/*" do 
     halt oauth.deny! if oauth.scope.include?("time-travel") # Only Superman can do that 
    end 

    get "/oauth/authorize" do 
     "client: #{oauth.client.display_name}\nscope: #{oauth.scope.join(", ")}\nauthorization: #{oauth.authorization}" 
    end 

    post "/oauth/grant" do 
     oauth.grant! "Batman" 
    end 

    post "/oauth/deny" do 
     oauth.deny! 
    end 


    # 5. Accessing a Protected Resource 

    before { @user = oauth.identity if oauth.authenticated? } 


    oauth_required "/user" 

    get "/user" do 
     @user 
    end 

    get "/list_tokens" do 
     oauth.list_access_tokens("Batman").map(&:token).join(" ") 
    end 
    end    
    end 
end 

然後我嘗試從終端使用捲曲獲得授權代碼:

curl -i http://localhost:4567/oauth/authorize -F response_type=code -F client_id=[the ID] -F client_secret=[the secret] -F redirect_uri=http://localhost:4567/oauth/showcode 

和Just我得到作爲響應:

HTTP/1.1 400 Bad Request 

的Content-Type:text/plain的 的Content-Length:20 連接:保持活躍 服務器:薄1.2.11代號蝙蝠糞瘋狂

失蹤重定向URL

你有什麼想法我做錯了什麼?謝謝!

回答

2

您的捲曲請求的結尾是:

-F redirect_uri=http://localhost:4567/oauth/showcode 

,但還沒有定義,在上面的代碼路徑,即地方是:

get "/oauth/showcode" do 

?這就是爲什麼錯誤是「缺少重定向URL」。