2011-10-26 39 views
2

設計/ omniauth成立,現在我想使用Facebook的JavaScript SDK登錄/索要權限,然後它們路由到我omniauth回調控制器。如何從Facebook獲取授權碼我的JavaScript SDK

這是我(的CoffeeScript)。

$('#fb-connect').live 'click', -> 
    FB.login ((response) -> 
    if response.authResponse 
     window.location = "https://stackoverflow.com/users/auth/facebook/callback?code=" + response.authResponse.signedRequest 
    else 
     console.log "User cancelled login or did not fully authorize." 
), scope: "email, offline_access" 

    false 

但是我收到了Invalid verification code format錯誤。我假設這是因爲代碼參數需要除簽名請求之外的其他東西?

更新

所以看起來我需要在授權碼通過,但我怎麼也找不到。 direct url example表明您可以指定response_type = code來獲取授權碼,但我不知道如何使用FB.api來做到這一點。有任何想法嗎?

http://www.facebook.com/dialog/oauth/? 
    scope=email,user_birthday& 
    client_id=123050457758183& 
    redirect_uri=http://www.example.com/response& 
    response_type=code 

回答

2

的文檔只是櫃面過這個問題,其他人絆倒你不需要使用設計/ omniauth ...以下作品完美

$('#fb-connect').live 'click', -> 
    FB.login ((response) -> 
    if response.authResponse 
     window.location = "https://stackoverflow.com/users/auth/facebook/callback 
    else 
     console.log "User cancelled login or did not fully authorize." 
), scope: "email, offline_access" 

    false 

編輯時,任何參數傳遞給控制器

如果使用的是較小的版本或獲取當兩個PARAM和代碼缺少以下內嵌JS拋出的異常周圍一個onclick工作。

<script> 
    function fb_authorise(){ 
    FB.login(function(response) { 
     if(response.authResponse) { 
     window.location = "https://stackoverflow.com/users/auth/facebook/callback?signed_request=<%= params[:signed_request]%>" 
     } 
    }, {scope: "email, offline_access"}); 
    }; 
</script> 

那麼,現在回答你原來的問題,你用的signed_request PARAM,然後加入與代碼URL =不signed_request

+1

這個我覺得是隻對omniauth 0.3.2,後來真(在時間我在0.3.0) – CodeWombat

0

使用客戶端身份驗證,您不需要使用'代碼'。你擁有的response.authResponse將會包含access_token。 FB.login()已經爲你做了所有這些。如果您有「window.location = ...」,用戶已登錄。

檢查文檔authentication - 確保您正在閱讀客戶端部分。還要檢查fb.login

相關問題