我可以導航到成功重定向到Facebook的主要url。我授予權限,並且我被重定向回到了回調網址。如果這個url只是簡單地返回類似'hello'的東西,那麼它工作正常並沒有錯誤。但撥打token = client.auth_code.get_token(@data[:code], :redirect_uri => redirect_uri)
會導致錯誤。Sinatra使用gem oauth2登錄到Facebook OAuth 2.0
4
A
回答
6
好吧終於得到了這個工作。被報告的錯誤是錯誤處理的一些奇怪的事情,並且與實際問題無關。問題是,oauth2寶石是通用的,你必須花費一些東西才能使它與Facebook一起工作。這些是你必須做的自述不同的東西(見GitHub上的問題70和75獲取更多信息)
您創建客戶端之前,你必須爲Facebook的響應註冊一個解析器:
OAuth2::Response.register_parser(:facebook, 'text/plain') do |body|
token_key, token_value, expiration_key, expiration_value = body.split(/[=&]/)
{token_key => token_value, expiration_key => expiration_value, :mode => :query, :param_name => 'access_token'}
end
您還可以設置客戶端的令牌網址上創造:
@client = OAuth2::Client.new(ENV['FACEBOOK_APP_ID'], ENV['FACEBOOK_APP_SECRET'], {:site => 'https://graph.facebook.com', :token_url => '/oauth/access_token'})
當OAuth的接收的響應,它使用你告訴它使用解析響應到一個哈希解析器。自定義:Facebook解析器確保哈希包含訪問令牌和過期字符串,並告訴它使用查詢模式,並且參數名稱爲access_token。如果沒有模式和param_name,則在訪問資源時,Oauth客戶端將嘗試在頭中而不是查詢字符串中發送訪問令牌。 Facebook期望訪問令牌位於網址中。如果沒有param_name,oauth客戶端會將其發送爲 https://graph.facebook.com/bearer_token=ABC。隨着PARAM_NAME,它是https://graph.facebook.com/access_token=ABC
最後,當您創建的accessToken對象時,一定要告訴它使用自定的解析器,像這樣:
token = client.auth_code.get_token(@data[:code], {:redirect_uri => redirect_uri, :parsed => :facebook})
總之,它看起來像:
require 'sinatra'
require 'oauth2'
require 'json'
class App < Sinatra::Base
configure do
set :views_folder, File.join($BP, 'views')
set :public_folder, File.join($BP, 'public')
end
before do
@data = JSON.parse(request.env["rack.input"].read) if request.request_method =~ /POST|PUT|DELETE/i
@data = params if request.request_method == 'GET'
end
before do
pass if (request.path_info == '/auth/facebook' || request.path_info == '/auth/facebook/callback')
redirect to('/auth/facebook') unless self.logged_in
end
get "/" do
request.request_method
end
def client
if [email protected]
OAuth2::Response.register_parser(:facebook, 'text/plain') do |body|
token_key, token_value, expiration_key, expiration_value = body.split(/[=&]/)
{token_key => token_value, expiration_key => expiration_value, :mode => :query, :param_name => 'access_token'}
end
@client = OAuth2::Client.new(ENV['FACEBOOK_APP_ID'], ENV['FACEBOOK_APP_SECRET'], {:site => 'https://graph.facebook.com', :token_url => '/oauth/access_token'})
end
@client
end
get '/auth/facebook' do
redirect client.auth_code.authorize_url(
:redirect_uri => redirect_uri,
:scope => 'email'
)
end
get '/auth/facebook/callback' do
token = client.auth_code.get_token(@data[:code], {:redirect_uri => redirect_uri, :parsed => :facebook})
user = token.get('/me').parsed
create_user user unless user_exists user
end
def redirect_uri
uri = URI.parse(request.url)
uri.path = '/auth/facebook/callback'
uri.query = nil
uri.to_s
end
end
相關問題
- 1. 使用Oauth 2.0登錄到Facebook
- 2. OAuth2 ::錯誤,登錄Oauth-Facebook的問題
- 3. Facebook使用oauth 2.0在Android上使用授權碼登錄
- 4. 處理Oauth 2.0-facebook gem error 100:已使用此授權碼
- 5. oauth token for sinatra oauth2-providers
- 6. 爲什麼不使用OAuth登錄Facebook?
- 7. Android- Oauth 2.0谷歌登錄
- 8. Facebook的OAuth登錄問題與Facebook SDK 2.0
- 9. Facebook登錄問題(的OAuth2)
- 10. 使用OAuth 2.0令牌登陸到Salesforce.com
- 11. dart oauth facebook twitter登錄
- 12. Facebook OAuth登錄錯誤
- 13. 使用Facebook登錄並使用oauth 2.0進行REST API調用認證
- 14. Xamarin.Forms OAuth/Facebook SDK登錄
- 15. 如何使用OAuth2自動化Facebook的登錄過程?
- 16. Oauth2 Facebook登錄在v2.2棄用後無法使用
- 17. 使用OAuth登錄
- 18. Facebook OAuth 2.0登錄圖標沒有顯示
- 19. Facebook OAuth 2.0問題
- 20. iPhone Objective C oauth 2.0登錄 - unsupported_grant_type
- 21. 如何檢查用戶是否使用「Google登錄」登錄(OAuth 2.0)
- 22. 登錄Sinatra?
- 23. 使用Facebook OAuth授權。如何登錄到Facebook授權我的應用程序?
- 24. 使用Google oAuth 2.0時刷新新用戶登錄頁面
- 25. 使用Manatsto Gem與Sinatra
- 26. 使用omniouth-facebook和devise登錄到facebook
- 27. 使用oauth-plugin gem在rspec測試中測試OAuth2請求
- 28. 控制的Facebook的OAuth登錄體驗
- 29. 如何將MVC 4 OpenId Google登錄遷移到OAuth 2.0或Google+登錄
- 30. Facebook OAuth在Ionic中登錄錯誤
如果任何人看到最佳做法違規,請讓我知道即時通訊設法得到更好的 – chris 2012-01-14 23:45:16
我會考慮在路線之前分組你的幫手方法,但除此之外,這對我來說看起來不錯。我正要着手學習如何自己做這個,所以謝謝你讓我困惑不已:-) – 2012-01-16 22:13:10