fb_util 0.0.7.1 → 0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/fb_util/fb_util_error.rb +17 -0
- data/lib/fb_util.rb +73 -13
- metadata +4 -3
@@ -0,0 +1,17 @@
|
|
1
|
+
class FBUtilError < Exception
|
2
|
+
attr_accessor :error_code, :body, :object
|
3
|
+
|
4
|
+
def initialize(error_code, body)
|
5
|
+
@error_code = error_code
|
6
|
+
@body = body
|
7
|
+
begin
|
8
|
+
@object = JSON.parse(body)
|
9
|
+
rescue JSON::ParserError
|
10
|
+
@object = {"error" => {"message" => "Response was not valid json. See the body for more information."}}
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def to_s
|
15
|
+
"Response code: #{error_code} => #{body}"
|
16
|
+
end
|
17
|
+
end
|
data/lib/fb_util.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'mechanize'
|
2
|
+
require 'fb_util/fb_util_error'
|
2
3
|
|
3
4
|
class FBUtil
|
4
5
|
@graph_url = nil
|
@@ -16,7 +17,7 @@ class FBUtil
|
|
16
17
|
end
|
17
18
|
end
|
18
19
|
|
19
|
-
# Get the basic account information from facebook
|
20
|
+
# Get the basic account information from facebook
|
20
21
|
def get_account_info
|
21
22
|
@account_info ||= get_request('/me')
|
22
23
|
end
|
@@ -52,33 +53,33 @@ class FBUtil
|
|
52
53
|
end
|
53
54
|
end
|
54
55
|
|
55
|
-
# Set an already existing image to be the users cover image
|
56
|
+
# Set an already existing image to be the users cover image
|
56
57
|
# picture_id: the facebook id of the image to use as the users cover image. This currently doesn't allow for an offset, but this will be available in the next version.
|
57
58
|
def set_as_cover_image(picture_id)
|
58
59
|
post_request('/me', {cover: picture_id, no_feed_story: 'true'})
|
59
60
|
end
|
60
61
|
|
61
|
-
# Post a clickable link to a users feed (works for youtube videos as well)
|
62
|
-
# link: the link (beginning with http:// or https://) that will be displayed in the users feed
|
63
|
-
# message: the message to be posted with the link on the feed page
|
62
|
+
# Post a clickable link to a users feed (works for youtube videos as well)
|
63
|
+
# link: the link (beginning with http:// or https://) that will be displayed in the users feed
|
64
|
+
# message: the message to be posted with the link on the feed page
|
64
65
|
def post_link(link, message)
|
65
66
|
post_request('/me/feed', {link: link, message: message})
|
66
67
|
end
|
67
68
|
|
68
69
|
# Post a reply to a status that is already existing on facebook
|
69
|
-
# status_id: the facebook id of the status to reply to
|
70
|
-
# message: the message to use in the reply
|
70
|
+
# status_id: the facebook id of the status to reply to
|
71
|
+
# message: the message to use in the reply
|
71
72
|
def reply_to_status(status_id, message)
|
72
73
|
post_request('/' + status_id + '/comments', {message: message})
|
73
74
|
end
|
74
75
|
|
75
|
-
# Delete a status
|
76
|
-
# status_id: the facebook id of the status to delete
|
76
|
+
# Delete a status
|
77
|
+
# status_id: the facebook id of the status to delete
|
77
78
|
def delete_status(status_id)
|
78
79
|
delete_request('/' + status_id)
|
79
80
|
end
|
80
81
|
|
81
|
-
# Get statuses from facebook page/account
|
82
|
+
# Get statuses from facebook page/account
|
82
83
|
def get_statuses
|
83
84
|
@statuses ||= get_request('/me/statuses')
|
84
85
|
# I have no idea why sometimes it uses the data index and sometimes it doesn't....
|
@@ -89,7 +90,7 @@ class FBUtil
|
|
89
90
|
end
|
90
91
|
end
|
91
92
|
|
92
|
-
# Get the insights of a page (**this doesn't work for profiles**)
|
93
|
+
# Get the insights of a page (**this doesn't work for profiles**)
|
93
94
|
def get_insights
|
94
95
|
@insights ||= get_request('/me/insights')
|
95
96
|
begin
|
@@ -175,6 +176,65 @@ class FBUtil
|
|
175
176
|
|
176
177
|
# These are the methods that don't need to be called with an instanciated class because they don't need an access token
|
177
178
|
class << self
|
179
|
+
# This method will generate the appropriate redirect for dialog oauth calls
|
180
|
+
# app_id: your app id from the facebook developer application
|
181
|
+
# redirect_uri: the uri that you want facebook to redirect to after the user authorizes your app (you can pass get variables in this if you need to pass variables back to your application i.e. http://example.com/auth?client_id=1)
|
182
|
+
# scope: comma seperate list of permissions your application is requesting (https://developers.facebook.com/docs/authentication/permissions/)
|
183
|
+
def generate_oauth_redirect(app_id, redirect_uri, scope)
|
184
|
+
return "https://www.facebook.com/dialog/oauth?client_id=" + app_id.to_s + "&redirect_uri=" + CGI.escape(redirect_uri.to_s) + '&scope=' + scope.to_s
|
185
|
+
end
|
186
|
+
|
187
|
+
# This method will get a long term access token. If you are getting the access_token for a facebook profile this will be valid for 60 days. If you are getting the access_token for a facebook page it be permanent.
|
188
|
+
# app_id: your app id assigned from facebook
|
189
|
+
# app_secret: your app secret assigned from facebook
|
190
|
+
# redirect_uri: A valid redirect uri is required to get access_tokens. You will not be redirected to this uri
|
191
|
+
# code: the code sent back from facebook after the user has authorized your application (generate_oauth_redirect will redirect back to your requested uri with the code needed)
|
192
|
+
def get_long_access_token(app_id, app_secret, redirect_uri, code)
|
193
|
+
begin
|
194
|
+
agent = Mechanize.new
|
195
|
+
short_access_page = agent.get("https://graph.facebook.com/oauth/access_token?client_id=#{app_id}&client_secret=#{app_secret}&redirect_uri=#{redirect_uri}&code=" + code)
|
196
|
+
short_access_token = short_access_page.body.split('&')[0].split('=')[1]
|
197
|
+
# This is where we convert the short access token into a long access token
|
198
|
+
long_token_page = agent.get("https://graph.facebook.com/oauth/access_token?client_id=#{app_id}&client_secret=#{app_secret}&grant_type=fb_exchange_token&fb_exchange_token=#{short_access_token}")
|
199
|
+
return {'access_token' => long_token_page.body.split('=')[1]}
|
200
|
+
rescue Mechanize::ResponseCodeError => e
|
201
|
+
raise FBUtilError.new(e.response_code, e.page.body)
|
202
|
+
end
|
203
|
+
end
|
204
|
+
|
205
|
+
# If you already have a short term access token this method will exchange that for a long term access token. 60 days for user accounts and indefinitely for profile pages
|
206
|
+
# short_access_token: the access token already assigned to the account. It must be a valid, non-expired access token
|
207
|
+
# app_id: your app id assigned from facebook
|
208
|
+
# app_secret: your app secret assigned from facebook
|
209
|
+
def get_long_from_short_access_token(short_access_token, app_id, app_secret)
|
210
|
+
begin
|
211
|
+
agent = Mechanize.new
|
212
|
+
long_token_page = agent.get("https://graph.facebook.com/oauth/access_token?client_id=#{app_id}&client_secret=#{app_secret}&grant_type=fb_exchange_token&fb_exchange_token=#{short_access_token}")
|
213
|
+
return {'access_token' => long_token_page.body.split('=')[1]}
|
214
|
+
rescue Mechanize::ResponseCodeError => e
|
215
|
+
raise FBUtilError.new(e.response_code, e.page.body)
|
216
|
+
end
|
217
|
+
end
|
218
|
+
|
219
|
+
# This method will get a short term access token for your application. Generally (1 - 2 hours)
|
220
|
+
# app_id: your app id assigned from facebook
|
221
|
+
# app_secret: your app secret assigned from facebook
|
222
|
+
# redirect_uri: A valid redirect uri is required to get access_tokens. You will not be redirected to this uri
|
223
|
+
# code: the code sent back from facebook after the user has authorized your application (generate_oauth_redirect will redirect back to your requested uri with the code needed)
|
224
|
+
def get_short_access_token(app_id, app_secret, redirect_uri, code)
|
225
|
+
begin
|
226
|
+
agent = Mechanize.new
|
227
|
+
short_access_page = agent.get("https://graph.facebook.com/oauth/access_token?client_id=#{app_id}&client_secret=#{app_secret}&redirect_uri=#{redirect_uri}&code=" + code)
|
228
|
+
if short_access_page.body.include?('expires')
|
229
|
+
return {'access_token' => short_access_page.body.split('&')[0].split('=')[1], 'expires' => short_access_page.body.split('&')[1].split('=')[1]}
|
230
|
+
else
|
231
|
+
return {'access_token' => short_access_page.body.split('=')[1]}
|
232
|
+
end
|
233
|
+
rescue Mechanize::ResponseCodeError => e
|
234
|
+
raise FBUtilError.new(e.response_code, e.page.body)
|
235
|
+
end
|
236
|
+
end
|
237
|
+
|
178
238
|
# This method will parse out a signed request using all the necessary validation required to find out if a facebook request is completely valid
|
179
239
|
# signed_request: the signed request passed in by facebook
|
180
240
|
# application_secret: the application secret assigned to your application form facebook
|
@@ -189,9 +249,9 @@ class FBUtil
|
|
189
249
|
elsif json['issued_at'] < Time.now.to_i - max_age
|
190
250
|
raise 'Signed request too old.'
|
191
251
|
elsif base64_url_decode(encoded_signature) != OpenSSL::HMAC.hexdigest('sha256', application_secret, encoded_json).split.pack('H*')
|
192
|
-
raise 'Invalid signature.'
|
252
|
+
raise 'Invalid signature.'
|
193
253
|
end
|
194
|
-
|
254
|
+
|
195
255
|
return json
|
196
256
|
end
|
197
257
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fb_util
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: '0.9'
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-07-02 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: mechanize
|
@@ -28,13 +28,14 @@ dependencies:
|
|
28
28
|
- !ruby/object:Gem::Version
|
29
29
|
version: '2.3'
|
30
30
|
description: A quick utility class to work with the facebook graph api and misc facebook
|
31
|
-
functions
|
31
|
+
functions, will be version 1 as soon as I figure out tests for this.
|
32
32
|
email: brennon@fritzandandre.com
|
33
33
|
executables: []
|
34
34
|
extensions: []
|
35
35
|
extra_rdoc_files: []
|
36
36
|
files:
|
37
37
|
- lib/fb_util.rb
|
38
|
+
- lib/fb_util/fb_util_error.rb
|
38
39
|
homepage: https://github.com/jbrennon/fb_util
|
39
40
|
licenses: []
|
40
41
|
post_install_message:
|