koala gem(Graph API)使用get_page方法检索结果页面时出现InvalidURIError

时间:2016-05-18 15:31:06

标签: ruby-on-rails ruby facebook facebook-graph-api koala

我正在使用Koala gem:https://github.com/arsduo/koala从facebook图形API中检索结果页面。

[编辑]我按如下方式构造@graph对象:

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

从Facebook获取数据后,我得到了一个结果列表。

我得到了这样的next_page_params:

next_page = @graph.next_page_params 

看起来像:

[\"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\"}]

现在我使用以下方式获取结果的下一页:

@graph.get_page(next_page)

这是我得到的错误:

`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]`

3 个答案:

答案 0 :(得分:0)

看起来您的问题可能就在这里

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

在github页面上,它表示直接用@graph调用get_connection,但是你将@graph设置为@facebook.get_connection(..)的返回值。而是将其改为

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

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

答案 1 :(得分:0)

这里有几件事:

  1. 你的'next_page_params'响应似乎是一个字符串,而它应该是一个数组。你确定这是你得到的回应吗?当方法尝试将其转换为要解析的URI时,这可能会导致错误。因此解析错误。您可以尝试使用eval(next_page)将其转换为实际数组(注意:eval被认为是不安全的)。

  2. 您使用的是哪种版本的考拉?在最新版本(v2.3.0)中,get_page方法仅适用于API对象(在您的情况下为@facebook),而不在连接对象(@graph)上。因此,您将使用:

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

  3. 您也可以尝试直接调用下一页,而无需先获取如下的参数:

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

答案 2 :(得分: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

希望这有帮助。