Facebook FQL Query with Ruby

2019-02-26 10:42发布

I'm trying to do a simple GET with ruby to the Facebook fql.query method without success.

The url is basically structured like this:

https://api.facebook.com/method/fql.query?query=SELECT total_count FROM link_stat WHERE url = "http://twitter.com/"&format=json

I've read in a few posts here on StackOverflow about how to make those requests, but even tho I keep getting:

/usr/lib/ruby/1.8/net/http.rb:560:in `initialize': getaddrinfo: Name or service not known (SocketError)

On the first line of http_get function.

def http_get(domain,path,params)
    return Net::HTTP.get(domain, "#{path}?".concat(params.collect { |k,v| "#{k}=#{CGI::escape(v.to_s)}" }.join('&'))) if not params.nil?
    return Net::HTTP.get(domain, path)
end

def getFacebookStats(url)

    params = {
        :query => 'SELECT total_count FROM link_stat WHERE url = "' + url + '"',
        :format => 'json'
    }

    http = http_get('https://api.facebook.com', '/method/fql.query', params)
    puts http

end

1条回答
冷血范
2楼-- · 2019-02-26 11:16

The http call accepts a host, not a URL:

def http_get(domain,path,params)
    path = unless params.blank
        path + "?" + params.collect { |k,v| "#{k}=#{CGI::escape(v.to_s)}" }.join('&')
      else
        path
    end
    request = Net::HTTP.get(domain, path)

end

def get_facebook_stats(url)

    params = {
        :query => 'SELECT total_count FROM link_stat WHERE url = "' + url + '"',
        :format => 'json'
    }

    http = http_get('api.facebook.com', '/method/fql.query', params)
    puts http

end

Please do not use camel case on method names on Ruby.

If you want to make HTTPS calls, you will have to use a different call:

require 'net/http'
require 'net/https'

http = Net::HTTP.new('somehost.com', 443)
http.use_ssl = true
path = '/login.html'

resp, data = http.get(path, nil)
查看更多
登录 后发表回答