2010-06-18 23 views
0

我收到以下URL信息,需要在rails中解析它。我已經檢查過請求和參數,但它不在那裏。在rails中解析#uris

「#」字符看起來似乎有些東西。

這裏的網址:

http://foo.bar.me/whoo#access_token=131268096888809%7C2.5BRBl_qt4xJ08n88ycbpZg__.3600.1276880400-100001151606930%7C0kJ1K-qoGBbDoGbLx6s4z5UEaxM. 

感謝任何指針。

+0

請更明確。你如何收到這個網址?你使用什麼代碼?究竟發生了什麼問題?等等...(另外,不需要褻瀆...) – thomasfedb 2010-06-18 15:35:35

+0

我正在嘗試爲http://appsbiddington.wordpress.com/2010/04/23/facebook- graph-api-getting-access-token/- 我按照概述重定向,但無法訪問令牌,因爲它位於# – z3cko 2010-06-18 15:46:29

+0

之後,如果您從wordpress獲得此URL,則是wordpress問題。因爲不像這樣渲染access_token。 – shingara 2010-06-18 15:59:53

回答

1

由於瀏覽器未將其發送到服務器,您將無法訪問'#'字符後面的部分。儘管你可以在客戶端使用它。

看來你正在嘗試使用不是你真正想要的javascript based authentication

我沒有使用此oauth2庫的任何問題。那麼你只需要在你的回調行動中檢查params[:code]

UPDATE:

這是我在實驗中使用與新的Facebook圖形API代碼的簡化版本:

# Accessible as facebook_url: 
# routes.rb: map.facebook '/facebook', :controller => 'facebook', :action => 'index' 
def index 
    oauth2 = OAuth2::Client.new(FB_API_KEY, FB_API_SECRET, :site => 'https://graph.facebook.com') 

    if current_user.facebook_token 
    # The user is already authenticated 
    fb = OAuth2::AccessToken.new(oauth2, current_user.facebook_sid) 
    result = JSON.parse(fb.get('/me')) 
    elsif params[:code] 
    # Here we get the access token from facebook 
    fb = oauth2.web_server.get_access_token(params[:code], :redirect_uri => facebook_url) 
    result = JSON.parse(fb.get('/me')) 
    current_user.facebook_id = result["id"] 
    current_user.facebook_token = fb.token.to_s 
    current_user.save 
    else 
    # The user is visiting this page for the first time. We redirect him to facebook 
    redirect_to oauth2.web_server.authorize_url(:redirect_uri => facebook_url, :scope => 'read_stream,publish_stream,offline_access') 
    end 
end 

你並不真正需要的任何東西爲它工作。

+0

很好的答案,非常感謝!澄清事情:它看起來像apache mod_rewrite去掉「#」。 – z3cko 2010-06-18 18:27:39

+1

它沒有被mod_rewrite剝離,#部分永遠不會到達服務器,它僅由客戶端瀏覽器解釋。 – 2010-06-18 18:53:49