forked from tmm1/sinbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
README
145 lines (101 loc) · 3.83 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
sinbook:
sintra facebook auth using new Graph url
modified 2011 by Anish Kumar (anishk123)
to use the latest facebook graph api
and ability to add refs to track users
original:
simple sinatra facebook extension in 300 lines of ruby
(c) 2009 Aman Gupta (tmm1)
=== Usage
require 'sinbook'
require 'sinatra'
facebook do
api_key '4579...cbb0'
secret '5106...2342'
app_id 81747826609
url 'http://apps.facebook.com/myappname'
callback 'http://myappserver.com'
end
#to login user with no extra permission
get '/' do
fb.require_login! #fb.require_login!('tracking_code') to track refs
"Hi <fb:name uid=#{fb[:user]} useyou=false />!"
end
#to login user with more permissions such as publish stream, email access etc.
get '/' do
fb.require_login_with_perms!('publish_stream,email')
#fb.require_login_with_perms!('publish_stream,email,xmpp_login', 'tracking_code') to track refs
"Hi <fb:name uid=#{fb[:user]} useyou=false />!"
end
=== Features
sinbook provides a simple `facebook` helper (also aliased to `fb`).
>> fb.valid?
=> true # valid (authenticated) request from facebook's servers
>> pp fb.params
{
:logged_out_facebook => false, # request came from a user logged into facebook
:added => true, # user is logged into our app
:user => 1234, # user's facebook uid
:friends => [1,2,3], # list of user's friends
...
}
>> fb[:user] # [] is aliased to params[]
=> 1234
>> fb.callback
=> 'http://apps.facebook.com/myappname'
>> fb.callback('/homepage')
=> 'http://apps.facebook.com/myappname/homepage'
>> fb.url('/images/static.gif')
=> 'http://myappserver.com/images/static.gif'
>> fb.appurl
=> 'http://apps.facebook.com/add.php?api_key=4579...cbb0'
>> fb.addurl
=> 'http://www.facebook.com/apps/application.php?id=81747826609'
>> fb.redirect('/welcome') # redirect using an fb:redirect tag
>> fb.require_login! # redirect to addurl page unless logged in
The helper can also be used to make API calls
>> fb.users.getInfo :uid => 1234, :fields => [:name]
=> [{'uid' => 1234, 'name' => 'Frank Sinatra'}]
>> fb.groups.get :uid => 1234
=> [{'name' => 'Sinatra Users'}]
>> fb.profile.setFBML :profile => 'hello world'
=> true
>> fb.profile.getFBML
=> 'hello world'
=== Other Options
facebook do
symbolize_keys true
end
>> fb.groups.get :uid => 1234
=> [{:name => 'Sinatra Users'}]
=== Standalone Usage
require 'sinbook'
fb = Sinbook.new(
:api_key => '4579...cbb0',
:secret => '5106...2342',
:app_id => 81747826609
)
>> fb.friends.get :uid => 1234, :session_key => 'user-session-key'
=> [4321,4567,9876]
=== Local Development
To develop locally, use ssh to setup a reverse tunnel to your external server.
For example, set your callback url to http://myserver.com:4567/, and run:
ssh -gNR 4567:localhost:4567 [email protected]
Then, simply launch sinatra on your local machine. Facebook will make requests to
http://myserver.com:4567/ which will be forwarded to port 4567 on your local machine.
For facebook connect, I generally add a CNAME on my domain for fb.myserver.com, and setup
nginx on my server with the following:
server {
server_name fb.myserver.com;
location / {
proxy_pass http://localhost:4567;
}
}
Make sure your connect url is set to 'http://myserver.com' and the base domain is set to 'myserver.com',
so that the fb.myserver.com subdomain works.
=== TODO
* Add a batch mode for api calls:
groups, pics = fb.batch do |b|
b.groups.get :uid => 123
b.users.getInfo :uids => 123, :fields => [:pic_square]
end