0

I've found facebook's 'Graph API Explorer' tool (https://developers.facebook.com/tools/explorer/) to be an incredibly easy way, welcoming (for beginners) & effective way to use facebook's graph API via its GUI.

I'd like to be able to use the koala gem to pass these generated URLs to facebook's api.

Right now, lets say I had a query like this

url = "me?fields=id,name,posts.fields(likes.fields(id,name),comments.fields(parent,likes.fields(id,name)),message)"

I'd like to be able to pass that directly into koala as a single string.

@graph.get_connections(url)

It doesn't like that so I separate out the uid and the ? operator like the gem seems to want

url = "fields=id,name,posts.fields(likes.fields(id,name),comments.fields(parent,likes.fields(id,name)),message)"
@graph.get_connections("me", url)

This however, returns an error as well:

Koala::Facebook::AuthenticationError: 
type: OAuthException, code: 2500, 
message: Unknown path components: /fields=id,name,posts.fields(likes.fields(id,name),comments.fields(parent,likes.fields(id,name)),message) [HTTP 400]

Currently this is where I am stuck. I'd like to continue using koala because I like the gem-approach to working with API's, especially when it comes to using OAuth & OAuth2.

UPDATE:

I'm starting to break down the request into pieces which the koala gem can handle, for example

posts = @graph.get_connections("me", "posts")
postids = posts.map { |p| p['id'] }
likes = postids.inject([]) {|ary, id| ary << @graph.get_connection(id, "likes") }

So that's a long way of getting two arrays, one of posts, one of like data.

But I'd quickly burn up my API requests limit in no time using this kind of approach.

I was kind of hoping I'd just be able to pass the whole string from the Graph API Explorer and just get what I wanted rather than having to manually parse all this stuff.

4

2 回答 2

3

我真的不知道你的- 这在Graph API Explorerposts.fields(likes.fields(id,name)中不起作用- 以及类似的东西,但我知道你可以这样做:

fb_api = Koala::Facebook::API.new(access_token)
fb_api.api("/me?fields=id,name,posts")
# => => {"id"=>"71170", "name"=>"My Name", "posts"=>{"paging"=>{"next"=>"https://graph.facebook.com/71170/posts?access_token=CAAEO&limit=25&until=13705022", "previous"=>"https://graph.facebook.com/711737070/posts?access_token=CAAEOTYMZD&limit=25&since=1370723&__previous=1"}, "data"=>[{"id"=>"71170_1013572471", "comments"=>{"count"=>0}, "created_time"=>"2013-06-09T08:03:43+0000", "from"=>{"id"=>"71170", "name"=>"My Name"}, "updated_time"=>"2013-06-09T08:03:43+0000", "privacy"=>{"value"=>""}, "type"=>"status", "story_tags"=>{"0"=>[{"id"=>"71170", "name"=>"  ", "length"=>8, "type"=>"user", "offset"=>0}]}, "story"=>"  likes a photo."}]}}

您将在哈希中收到您要求的内容。

于 2013-06-12T02:06:07.350 回答
0

有时,您必须将 nil 作为参数传递给 koala:

result += graph_api.batch do |batch_api|
  facebook_page_ids.each do |facebook_page_id|
    batch_api.get_connections(facebook_page_id, nil, {"fields"=>"posts"})
  end
end
于 2013-08-29T21:04:34.620 回答