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.

有帮助吗?

解决方案

I don't really know about your posts.fields(likes.fields(id,name) -this does not work in the Graph API Explorer- and stuff like that but I know you can do this:

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."}]}}

And you will receive in a hash what you asked for.

其他提示

From time to time, you must pass nil as a param to 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
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top