0 | 0 |
# OmniAuth Facebook [![Build Status](http://travis-ci.org/mkdynamic/omniauth-facebook.png?branch=master)](http://travis-ci.org/mkdynamic/omniauth-facebook)
|
1 | 1 |
|
2 | 2 |
This gem contains the Facebook strategy for OmniAuth 1.0.
|
|
3 |
|
|
4 |
Supports the OAuth 2.0 server-side flow. Read the Facebook docs for more details: http://developers.facebook.com/docs/authentication
|
3 | 5 |
|
4 | 6 |
## Installing
|
5 | 7 |
|
6 | 8 |
Add to your `Gemfile`:
|
7 | 9 |
|
8 | 10 |
```ruby
|
9 | |
gem 'omniauth-facebook', '~> 1.0.0.rc1'
|
|
11 |
gem 'omniauth-facebook'
|
10 | 12 |
```
|
11 | 13 |
|
12 | 14 |
Then `bundle install`.
|
13 | 15 |
|
14 | |
## Supported Flows
|
|
16 |
## Usage
|
15 | 17 |
|
16 | |
Supports the Server-side Flow as described in the the Facebook docs:
|
17 | |
http://developers.facebook.com/docs/authentication
|
|
18 |
`OmniAuth::Strategies::Facebook` is simply a Rack middleware. Read the OmniAuth 1.0 docs for detailed instructions: https://github.com/intridea/omniauth.
|
18 | 19 |
|
19 | |
**Pending:** Supports the Client-side Flow via parsing out the verification code from the signed request cookie.
|
|
20 |
Here's a quick example, adding the middleware to a Rails app in `config/initializers/omniauth.rb`:
|
20 | 21 |
|
21 | |
## Ruby
|
|
22 |
```ruby
|
|
23 |
Rails.application.config.middleware.use OmniAuth::Builder do
|
|
24 |
provider :facebook, ENV['FACEBOOK_KEY'], ENV['FACEBOOK_SECRET'], :scope => 'email', :display => 'popup'
|
|
25 |
end
|
|
26 |
```
|
22 | 27 |
|
23 | |
Tested with the following Ruby versions:
|
|
28 |
## Configuring
|
|
29 |
|
|
30 |
You can configure several options, which you pass in to the `provider` method via a `Hash`:
|
|
31 |
|
|
32 |
* `scope`: A comma-separated list of permissions you want to request from the user. See the Facebook docs for a full list of available permissions: http://developers.facebook.com/docs/reference/api/permissions. Default: `email,offline_access`
|
|
33 |
* `display`: The display context to show the authentication page. Options are: `page`, `popup`, `iframe`, `touch` and `wap`. Read the Facebook docs for more details: http://developers.facebook.com/docs/reference/dialogs#display. Default: `page`
|
|
34 |
|
|
35 |
For example, to request `email`, `offline_access` and `read_stream` permissions and display the authentication page in a popup window:
|
|
36 |
|
|
37 |
```ruby
|
|
38 |
Rails.application.config.middleware.use OmniAuth::Builder do
|
|
39 |
provider :facebook, ENV['FACEBOOK_KEY'], ENV['FACEBOOK_SECRET'], :scope => 'email,offline_access,read_stream', :display => 'popup'
|
|
40 |
end
|
|
41 |
```
|
|
42 |
|
|
43 |
*NB.* If you want to set the `display` format on a per-request basis, you can just pass it to the OmniAuth request phase URL, for example: `/auth/facebook?display=popup`.
|
|
44 |
|
|
45 |
## Authentication Hash
|
|
46 |
|
|
47 |
Here's an example *Authentication Hash* available in `request.env['omniauth.auth']`:
|
|
48 |
|
|
49 |
```ruby
|
|
50 |
{
|
|
51 |
:provider => 'facebook',
|
|
52 |
:uid => '1234567',
|
|
53 |
:info => {
|
|
54 |
:nickname => 'jbloggs',
|
|
55 |
:email => 'joe@bloggs.com',
|
|
56 |
:name => 'Joe Bloggs',
|
|
57 |
:first_name => 'Joe',
|
|
58 |
:last_name => 'Bloggs',
|
|
59 |
:image => 'http://graph.facebook.com/1234567/picture?type=square',
|
|
60 |
:urls => { :Facebook => 'http://www.facebook.com/jbloggs' },
|
|
61 |
:location => 'Palo Alto, California'
|
|
62 |
},
|
|
63 |
:credentials => {
|
|
64 |
:token => 'ABCDEF...',
|
|
65 |
:expires_at => 1321747205, # when the access token expires
|
|
66 |
:expires => true # if you request `offline_access` this will be false
|
|
67 |
},
|
|
68 |
:extra => {
|
|
69 |
:raw_info => {
|
|
70 |
:id => '1234567',
|
|
71 |
:name => 'Joe Bloggs',
|
|
72 |
:first_name => 'Joe',
|
|
73 |
:last_name => 'Bloggs',
|
|
74 |
:link => 'http://www.facebook.com/jbloggs',
|
|
75 |
:username => 'jbloggs',
|
|
76 |
:location => { :id => '123456789', :name => 'Palo Alto, California' },
|
|
77 |
:gender => 'male',
|
|
78 |
:email => 'joe@bloggs.com',
|
|
79 |
:timezone => -8,
|
|
80 |
:locale => 'en_US',
|
|
81 |
:verified => true,
|
|
82 |
:updated_time => '2011-11-11T06:21:03+0000'
|
|
83 |
}
|
|
84 |
}
|
|
85 |
}
|
|
86 |
```
|
|
87 |
|
|
88 |
The precise information available may depend on the permissions which you request.
|
|
89 |
|
|
90 |
## Supported Rubies
|
|
91 |
|
|
92 |
Actively tested with the following Ruby versions:
|
24 | 93 |
|
25 | 94 |
- MRI 1.9.3
|
26 | 95 |
- MRI 1.9.2
|