├── .gitignore
├── .ruby-version
├── Gemfile
├── Gemfile.lock
├── README.md
├── config.ru
└── lib
└── simple-ruby-facebook-example.rb
/.gitignore:
--------------------------------------------------------------------------------
1 | .bundle/
2 | .DS_Store
3 |
--------------------------------------------------------------------------------
/.ruby-version:
--------------------------------------------------------------------------------
1 | 2.1.3
2 |
--------------------------------------------------------------------------------
/Gemfile:
--------------------------------------------------------------------------------
1 | source 'https://rubygems.org'
2 |
3 | gem 'sinatra'
4 | gem 'koala'
5 | gem 'shotgun'
6 |
--------------------------------------------------------------------------------
/Gemfile.lock:
--------------------------------------------------------------------------------
1 | GEM
2 | remote: https://rubygems.org/
3 | specs:
4 | addressable (2.3.8)
5 | faraday (0.9.1)
6 | multipart-post (>= 1.2, < 3)
7 | koala (2.0.0)
8 | addressable
9 | faraday
10 | multi_json
11 | multi_json (1.11.0)
12 | multipart-post (2.0.0)
13 | rack (1.6.0)
14 | rack-protection (1.5.3)
15 | rack
16 | shotgun (0.9.1)
17 | rack (>= 1.0)
18 | sinatra (1.4.6)
19 | rack (~> 1.4)
20 | rack-protection (~> 1.4)
21 | tilt (>= 1.3, < 3)
22 | tilt (2.0.1)
23 |
24 | PLATFORMS
25 | ruby
26 |
27 | DEPENDENCIES
28 | koala
29 | shotgun
30 | sinatra
31 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | **This is just a simple example for connecting to facebook with OAUTH2.
2 | With this you can do all the reading and writing stuff to facebook.
3 | See the Koala Docs for more.**
4 |
5 | ### GETTING STARTED
6 |
7 | 1. Clone the repository
8 | 2. Run bundle inside the folder
9 | 3. Change APP_ID, APP_SECRET (get this from facebook by registering an app)
10 | 4. Add an URL like local.myapp.com pointing to 127.0.0.1 in your `/etc/hosts` file
11 | 5. Add local.myapp.com to your App Domains list on facebook
12 | 6. Set a long and random string as cookie secret (Line: 12)
13 | 7. Start your Server with `$ shotgun`
14 | 8. point your browser to `http://local.myapp.com:9393/` and click login
15 |
16 | ### DOING MORE
17 |
18 | At this point you can make calls to the Facebook API (there are some examples in the comment): https://github.com/benben/simple-ruby-facebook-example/blob/master/lib/simple-ruby-facebook-example.rb#L17-L23
19 |
20 | From that on, its better to read the [Koala documentation](https://github.com/arsduo/koala), use the [Graph Explorer from Facebook](https://developers.facebook.com/tools/explorer) and read the [Facebook API Documentation](https://developers.facebook.com/docs/graph-api/) to get an idea what you can do after the
21 | authentication.
22 |
23 | ### KUDOS
24 |
25 | Made with the help of this two awesome gems:
26 | * Sinatra -> http://www.sinatrarb.com/
27 | * Koala -> http://github.com/arsduo/koala
28 |
29 | ### LICENSE
30 |
31 | THE BEER-WARE LICENSE
32 |
33 | ben [at] nerdlabor [dot] de wrote this file. As long as you retain this notice you
34 | can do whatever you want with this stuff. If we meet some day, and you think
35 | this stuff is worth it, you can buy me a beer in return Benjamin Knofe http://benjaminknofe.com
36 |
37 |
38 |
--------------------------------------------------------------------------------
/config.ru:
--------------------------------------------------------------------------------
1 | require './lib/simple-ruby-facebook-example'
2 | run SimpleRubyFacebookExample
3 |
--------------------------------------------------------------------------------
/lib/simple-ruby-facebook-example.rb:
--------------------------------------------------------------------------------
1 | #let Bundler handle all requires
2 | require 'bundler'
3 | Bundler.require(:default)
4 |
5 | # register your app at facebook to get those infos
6 | # your app id
7 | APP_ID = 1234567890
8 | # your app secret
9 | APP_SECRET = '76dhf8656a75...'
10 |
11 | class SimpleRubyFacebookExample < Sinatra::Application
12 |
13 | use Rack::Session::Cookie, secret: 'PUT_A_GOOD_SECRET_IN_HERE'
14 |
15 | get '/' do
16 | if session['access_token']
17 | 'You are logged in! Logout'
18 | # do some stuff with facebook here
19 | # for example:
20 | # @graph = Koala::Facebook::GraphAPI.new(session["access_token"])
21 | # publish to your wall (if you have the permissions)
22 | # @graph.put_wall_post("I'm posting from my new cool app!")
23 | # or publish to someone else (if you have the permissions too ;) )
24 | # @graph.put_wall_post("Checkout my new cool app!", {}, "someoneelse's id")
25 | else
26 | 'Login'
27 | end
28 | end
29 |
30 | get '/login' do
31 | # generate a new oauth object with your app data and your callback url
32 | session['oauth'] = Koala::Facebook::OAuth.new(APP_ID, APP_SECRET, "#{request.base_url}/callback")
33 | # redirect to facebook to get your code
34 | redirect session['oauth'].url_for_oauth_code()
35 | end
36 |
37 | get '/logout' do
38 | session['oauth'] = nil
39 | session['access_token'] = nil
40 | redirect '/'
41 | end
42 |
43 | #method to handle the redirect from facebook back to you
44 | get '/callback' do
45 | #get the access token from facebook with your code
46 | session['access_token'] = session['oauth'].get_access_token(params[:code])
47 | redirect '/'
48 | end
49 | end
50 |
51 |
--------------------------------------------------------------------------------