Last active
          April 27, 2016 09:56 
        
      - 
      
- 
        Save retgoat/93a83f3ed620b9f99242cb34c70165e2 to your computer and use it in GitHub Desktop. 
    OAuth with Koala
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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":"[email protected]","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}} | |
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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 | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | APP_KEY = 123456789 | |
| APP_SECRET = "FooBarBaz" | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | AwesomeApp::Application.routes.draw do | |
| get "fb/callback" | |
| get "fb/fb_get_info" | |
| get "fb/fb_code" | |
| root 'index#index' | |
| end | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
Please note that just an exampe and it completely untested.