問題我有使用https://github.com/intridea/oauth2 Rails項目。在我的Rails應用程序,我有以下代碼:使用OAuth 2寶石
ApplicationController.rb
def facebook_client
OAuth2::Client.new(FacebookOauthCredentials::APP_ID, FacebookOauthCredentials::APP_SECRET, :site => 'https://graph.facebook.com')
end
FacebookController.rb
def facebook_session_create(poster, featured_item)
redirect_to facebook_client.web_server.authorize_url(:scope => 'publish_stream', :redirect_uri => "http://localhost:3000/facebook/facebook_callback")
end
def facebook_callback
if(params[:code])
begin
access_token = facebook_client.web_server.get_access_token(params[:code], :redirect_uri => "http://localhost:3000/facebook/facebook_callback")
access_token.post('/me/feed', "testing #{rand(1000)}")
rescue OAuth2::HTTPError => e
render :text => e.response.body
end
end
end
我每次運行此代碼我得到這樣的迴應:
{"error":{"type":"OAuthException","message":"Error validating verification code."}}
但是,我用在了OAuth2寶石的自述文件中提供的西納特拉的應用程序,它工作正常。
def client
OAuth2::Client.new(FacebookOauthCredentials::APP_ID, FacebookOauthCredentials::APP_SECRET, :site => 'https://graph.facebook.com')
end
get '/auth/facebook' do
redirect client.web_server.authorize_url(
:redirect_uri => redirect_uri,
:scope => 'publish_stream'
)
end
get '/auth/facebook/callback' do
access_token = client.web_server.get_access_token(params[:code], :redirect_uri => redirect_uri)
begin
user = JSON.parse(access_token.post('/me/feed', :message => "testing # {rand(10000)}"))
rescue Exception => e
return e.response.body
end
user.inspect
end
def redirect_uri
uri = URI.parse(request.url)
uri.path = '/auth/facebook/callback'
uri.query = nil
uri.to_s
end
我試着重現使用irb的步驟,但我一個HTTP 400錯誤。我不確定它是否出於同Rails應用程序相同的原因,或者是因爲我正在進行控制檯和Web瀏覽器操作的混合操作。任何建議將不勝感激。
你有沒有很好的配置你的Facebook應用程序的有效回調網址,你使用這個回調url? – shingara 2011-01-27 09:10:30