Search code examples
ruby-on-railsfacebook-graph-apirails-apiomniauth-facebook

How to get omniauth hash if I already have the access_token?


Currently I have it implemented like so.

def auth_hash
    url = URI.parse('https://graph.facebook.com/me')

    url.query = URI.encode_www_form('access_token' => @token, 'fields' => 'email, name, first_name, last_name, gender')
    req = Net::HTTP::Get.new url.request_uri

    http = Net::HTTP.new(url.host, url.port)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    response = nil
    http.start do |get|
      response = get.request(req)
    end
    response
end

My IOS app does the callbacks and sends the access token to authenticated with our server.

I use the method above and it returns the hash from the graph api and I get the user info perfectly.

This method that I've used but it's not utilizing omniauth. So my question is there a way to retrieve the omniauth hash using only the generated access_token (the token is generated by the facebook sdk and sends it to the server)?

I'm just trying to make sure I'm not reinventing the wheel using this method.


Solution

  • There are few ruby gems for authentication with Facebook and retrieving data from it.

    Please refer this link : https://github.com/arsduo/koala , https://github.com/nov/fb_graph2

    And for google use can use refer this link: https://github.com/zquestz/omniauth-google-oauth2