skip to Main Content

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


  1. 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/

    Login or Signup to reply.
  2. 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:

    Page Public Content Access
    Allows read-only access to public data on Pages for which you lack the manage_page login permission. Readable data includes business metadata, public comments, posts, and reviews.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search