开发者

Check if a given username is taken on Facebook

How to check if a username is already in use on Facebook?

My solution was trying to access http://www.facebook.com/USER and check the http headers (200 = OK; 404 = NOT FOUND). I could use this code:

require 'open-uri'
require 'net/http'开发者_如何学编程

def remote_file_exists?(url,httpcode)
  url = URI.parse(url)
  Net::HTTP.start(url.host, url.port) do |http|
    return http.head(url.request_uri).code == httpcode
  end
end

The problem is that Facebook always returns 302 (Found), then redirects to https://www.facebook.com/USER.

I can require net/https and create a new function:

def https_url_exists? (url,httpcode)
  url = URI.parse(url)
  net = Net::HTTP.new(url.host, url.port) 
  net.use_ssl = true
  net.verify_mode = OpenSSL::SSL::VERIFY_NONE
  net.start do |http|
    return (http.head(url.request_uri).code == httpcode)
  end
end

Now the problem is that some users use dots on their usernames. For example, username might be user.name. Facebook use redirections for this.

What's the best way to check if USERNAME exists on facebook? How to get USER.NAME if USERNAME redirects to it?


You can use https://graph.facebook.com/username. This will return a json response with info to see if it exists as well as enough information to identify it as a user or page.

Once you have a valid user you can get user name First,Last info using:

https://graph.facebook.com/{userId}?fields=first_name,last_name
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜