I am trying to scrape a public facebook page using facebook api, but every time i sent request it return
HTTP Error 400: Bad Request
I am putting correct access token, which is app id + “|” + app_secret. But still return Bad request. For safety reason I hide my app id and app secret code.
import facebook
import urllib3
import requests
from urllib.request import urlopen, Request
import json
import datetime
import csv
import time
app_id = 'facebook_app_id'
app_secret = 'facebook_app_secret'
access_token = app_id + "|" + app_secret
fb_page = "manchesterunited"
base = "https://graph.facebook.com/v3.2"
node = "/" + fb_page
parameters = "/?
fields=message,link,created_time,type,name,id,likes.limit(1).summary(true),comments.limit(1).summary(true),shares&limit=%s&access_token=%s" % (100, access_token)
url = base + node + parameters
def request_until_succeed(url):
req = Request(url)
success = False
while success is False:
try:
response = urlopen(req)
if response.getcode() == 200:
success = True
except Exception as e:
print(e)
time.sleep(5)
print("Error for URL %s: %s" % (url, datetime.datetime.now()))
return response.read()
test_status = json.loads(request_until_succeed(url))["data"][0]
print (json.dumps(test_status, indent=4, sort_keys=True))
2
Answers
With a simple App Access Token and a Public Page you do not own, you first need to get approved for Page Public Content Access: https://developers.facebook.com/docs/apps/review/feature/#reference-PAGES_ACCESS
Alternatively, for testing, you can use a Page Token of a Page you own.
More information about Tokens: https://developers.facebook.com/docs/facebook-login/access-tokens/
You can not access public pages any more without getting your app reviewed for this upfront.
https://developers.facebook.com/docs/apps/review/feature#reference-PAGES_ACCESS: