2016-05-18 15 views
3

我使用考拉寶石:https://github.com/arsduo/koala從facebook圖形API檢索結果頁面。考拉寶石(Graph API)使用get_page方法檢索結果頁時InvalidURIError

[編輯]我構造@graph對象如下:

@facebook = Koala::Facebook::API.new(access_token) 
@graph = @facebook.get_connection(..) 

從Facebook獲取數據後,我得到的結果的列表。

我得到next_page_params這樣的:

next_page = @graph.next_page_params 

它看起來像:

@graph.get_page(next_page) 

[\"v2.6/496090827168552/members\", {\"fields\"=>\"about,age_range,bio,cover,devices,email,education,first_name,gender,hometown,id,interested_in,last_name,languages,link,location,middle_name,name,political,picture.type(large),relationship_status,religion,work,website\", \"limit\"=>\"30\", \"icon_size\"=>\"16\", \"access_token\"=>\"EAAIeQOKC6YjJE3GUvyHqakVaIZCF1MY4jo5YtQ0qt2DFNPRa3O6akOXUMdx9eOozAFSOIZD\", \"offset\"=>\"30\", \"__after_id\"=>\"enc_AdACS2GnjoUp9fYXSa8maRmdCZAYMNRR7fqHpQG\"}] 

現在我使用獲取結果的下一頁這是我得到的錯誤:

`URI::InvalidURIError: bad URI(is not URI?): [%22v2.6/496090827168552/members%22,%20%7B%22fields%22=%3E%22about,age_range,bio,cover,devices,email,education,first_name,gender,hometown,id,interested_in,last_name,languages,link,location,middle_name,name,political,picture.type(large),relationship_status,religion,work,website%22,%20%22limit%22=%3E%2230%22,%20%22icon_size%22=%3E%2216%22,%20%22access_token%22=%3E%22CqXUMdx9eOozAHJl2cS4czacDnIwvEB96RCb1FSOIZD%22,%20%22offset%22=%3E%2230%22,%20%22__after_id%22=%3E%22enc_AdACS2GCCpmD1SFiHnmP0lpr0yiW8maRmdCZAYMNRR7fqHpQG%22%7D]` 
+0

你如何構建'@ graph'對象?看來你是在錯誤的對象上調用'next_page_params'。 – BoraMa

+0

你試圖瀏覽什麼API請求?即你想要列出什麼成員? – BoraMa

+0

@BoraMa謝謝,它是爲團體成員。用facebook Api

回答

0

它看起來像你的問題可能在這裏

@facebook = Koala::Facebook::API.new(access_token) 
@graph = @facebook.get_connection(..) 

在GitHub的頁面,它說叫get_connection直接與@graph但要設置@graph到@facebook.get_connection(..)返回值。相反,它更改爲

@graph = Koala::Facebook::API.new(access_token) 
@graph.get_connection(..) 

來源:https://github.com/arsduo/koala#graph-api

+0

這實質上是OP在做什麼。只是他將API實例存儲在'@facebook'中,而不是'@graph'。但最終他調用了API對象本身的get_connection(在他的案例中是'@facebook'),然後將響應存儲在'@graph'對象中。你的建議似乎只是改變變量名稱,但不會有任何影響。 – Sam

0

夫婦的東西在這裏:

  1. 你'next_page_params的迴應似乎是一個字符串,而它應該是一個數組。你確定這是你得到的迴應嗎?當該方法嘗試將其轉換爲要解析的URI時,這可能會導致錯誤。因此解析錯誤。您可以嘗試使用eval(next_page)將其轉換爲實際數組(注意:eval被認爲是不安全的)。

  2. 您正在使用哪種版本的考拉?在最新的(v2.3.0)中,get_page方法僅在API對象(您的案例中的@facebook)上可用,而不在連接對象(@graph)上可用。因此,你可以使用:

    @facebook = ::考拉實:: API.new(的access_token) @graph = @ facebook.get_connection(..) next_page = @ graph.next_page_params @ facebook.get_page( next_page)

  3. 您也可以嘗試直接調用下一個頁面,而無需先取出PARAMS,如:

    @facebook = ::考拉實:: API.new(的access_token) @graph = @facebook。 get_connection(..) @ graph.next_page

0

我認爲問題就像其他人已經提到的一樣。您可能會與@graph,@facebookresults混淆。

以下是我如何使用Rails實現用戶的Facebook好友列表。

controller/feeds_controller.rb 
def do_sth_with_facebook_friends 
facebook = Facebook.new(current_user) 
facebook_friends = facebook.friends(params[:next_page]) 
... 
@next_page = facebook_friends.next_page_params 
//@next_page will be used as params[:next_page] when you send another request to load the next page 
... 
end 

lib/facebook.rb 
def initialize(user) 
@graph = Koala::Facebook::API.new(user.facebook_token) 
end 
def friends(page) 
page ? @graph.get_page(page) : @graph.get_connections('me', 'friends') 
end 

希望這會有所幫助。