Skip to content

Instantly share code, notes, and snippets.

@retgoat
Last active Apr 27, 2016
Embed
What would you like to do?
OAuth with Koala
GET http://localhost:3000/fb/fb_code
RESPONSE
{"redirect_url":"https://www.facebook.com/dialog/oauth?client_id=893637180663238\u0026redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Ffb%2Fcallback"}
----
redirect to https://www.facebook.com/dialog/oauth?client_id=893637180663238\u0026redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Ffb%2Fcallback
redirect to callback_url
GET http://localhost:3000/fb/callback?code=FB_AUTH_CODE
RESPONSE
{"access_token":"FB_ACCESS_TOKEN"}
----
GET http://localhost:3000/fb/fb_get_info?access_token=FB_ACCESS_TOKEN
RESPONSE
{"user_data":{"id":"4492344324865","email":"my_fake_email@gmail.com","first_name":"Roman","gender":"male","last_name":"Sotnikov","link":"https://www.facebook.com/app_scoped_user_id/4492344324865/","locale":"en_US","name":"Roman Sotnikov","timezone":6,"updated_time":"2015-05-18T05:19:54+0000","verified":true}}
class FbController < ApplicationController
before_action :init_client, only: [:fb_code, :callback]
def fb_code
url = @koala.url_for_oauth_code
render json: { redirect_url: url}
end
def fb_get_info
data = Koala::Facebook::API.new(params[:access_token]).get_object("me")
render json: { user_data: data }
rescue Koala::Facebook::AuthenticationError => e
render json: { status: :error, message: e.message }
end
def callback
access_token = @koala.get_access_token(params[:code])
render json: { access_token: access_token }
end
def init_client
@koala ||= Koala::Facebook::OAuth.new(APP_KEY,
APP_SECRET,
"http://localhost:3000/fb/callback")
end
end
APP_KEY = 123456789
APP_SECRET = "FooBarBaz"
AwesomeApp::Application.routes.draw do
get "fb/callback"
get "fb/fb_get_info"
get "fb/fb_code"
root 'index#index'
end
@retgoat
Copy link
Author

retgoat commented Apr 27, 2016

Please note that just an exampe and it completely untested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment