Skip to content

Commit b634237

Browse files
committed
Use App Access Token generated by GET for all API requests
we have been using 'app_id|app_secret' as app access token but https://developers.facebook.com/blog/post/2017/07/18/graph-api-v2.10/ there was announcement 'On October 16, 2017 we will require an access token to fetch Page videos, posts and comments for all Graph API versions.' so I thought there might be problem with current method. https://developers.facebook.com/docs/facebook-login/access-tokens#apptoken that was why I add this commit. After this change it should use following GET request to generate an app access token, each time we run #posts, #videos GET /oauth/access_token ?client_id={app-id} &client_secret={app-secret} &grant_type=client_credentials
1 parent 6294e76 commit b634237

File tree

2 files changed

+12
-24
lines changed

2 files changed

+12
-24
lines changed

lib/funky/connections/api.rb

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module Funky
88
module Connection
99
class API < Base
1010
def self.fetch_all(path_query)
11-
uri = URI "https://#{host}/v2.9/#{path_query}&limit=100&access_token=#{app_id}%7C#{app_secret}"
11+
uri = URI "https://#{host}/v2.9/#{path_query}&limit=100&access_token=#{app_access_token}"
1212
fetch_data_with_paging_token(uri)
1313
end
1414

@@ -24,7 +24,7 @@ def self.fetch_data_with_paging_token(uri)
2424
end
2525

2626
def self.fetch(path_query, is_array: false)
27-
uri = URI "https://#{host}/v2.8/#{path_query}&limit=100&access_token=#{app_id}%7C#{app_secret}"
27+
uri = URI "https://#{host}/v2.8/#{path_query}&limit=100&access_token=#{app_access_token}"
2828
is_array ? fetch_multiple_pages(uri).uniq : json_for(uri)
2929
rescue URI::InvalidURIError
3030
raise Funky::ContentNotFound, "Invalid URL"
@@ -66,14 +66,14 @@ def self.fetch_multiple_pages(uri)
6666
def self.request(id:, fields:)
6767
uri = URI::HTTPS.build host: host,
6868
path: "/v2.8/#{id}",
69-
query: "access_token=#{app_id}%7C#{app_secret}&fields=#{fields}"
69+
query: "access_token=#{app_access_token}&fields=#{fields}"
7070
response_for(get_http_request(uri), uri)
7171
end
7272

7373
def self.batch_request(ids:, fields:)
7474
uri = URI::HTTPS.build host: host,
7575
path: "/",
76-
query: "include_headers=false&access_token=#{app_id}%7C#{app_secret}"
76+
query: "include_headers=false&access_token=#{app_access_token}"
7777
batch = create_batch_for ids, fields
7878
http_request = post_http_request uri
7979
http_request.set_form_data batch: batch.to_json
@@ -94,6 +94,14 @@ def self.app_secret
9494
Funky.configuration.app_secret
9595
end
9696

97+
def self.app_access_token
98+
@app_access_token ||= begin
99+
uri = URI::HTTPS.build host: host, path: "/v2.8/oauth/access_token",
100+
query: URI.encode_www_form({client_id: app_id, client_secret: app_secret, grant_type: 'client_credentials'})
101+
Funky::Connection::API.json_for(uri)[:access_token]
102+
end
103+
end
104+
97105
def self.post_http_request(uri)
98106
Net::HTTP::Post.new uri
99107
end

spec/pages/videos_spec.rb

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,26 +34,6 @@
3434
end
3535
end
3636

37-
context 'given a page with hundreds of videos' do
38-
let(:page_id) { nextflix_page_id }
39-
40-
# NOTE: This test fails if we only strictly followed the Facebook
41-
# documentation of fetching pages with timestamp-based pagination.
42-
specify 'includes the oldest video of the page' do
43-
expect(videos.map {|v| v.id}).to include '68196585394'
44-
end
45-
end
46-
47-
context 'given another page with hundreds of videos' do
48-
let(:page_id) { nbc_page_id }
49-
50-
# NOTE: This test fails if we only strictly followed the Facebook
51-
# documentation of fetching pages with timestamp-based pagination.
52-
specify 'includes the oldest video of the page' do
53-
expect(videos.map {|v| v.id}).to include '10152197716420746'
54-
end
55-
end
56-
5737
context 'given a request that raises' do
5838
let(:response) { Net::HTTPServerError.new nil, nil, nil }
5939
let(:response_body) { '{"name":"Fullscreen"}' }

0 commit comments

Comments
 (0)