Simple batch request for Graph API returning Unsupported Post Request error

695 Views Asked by At

I'm trying to get public engagement metrics via the Graph API for a list of links. Since there are a lot of them, a batch request is necessary to avoid hitting the rate limits. Using the engagement endpoint for links and the batch API guide provided by Facebook, I formatted the batch request as a list of dictionaries and submitted via POST instead of get.

But when I run my code (see below) I get an Unsupported Post Request error.

I'm behind and exhaused and any help would be greately appreciated.

Here's my code:

import requests
import json 
from fbauth import fbtoken

link1 ='https://www.nytimes.com/2018/07/02/world/europe/angela-merkel-migration-coalition.html'
link2 ='https://www.nytimes.com/2018/07/02/world/europe/trump-nato.html'

# input dictionary for request
batch=[{"method":"GET", "relative_url": '/v3.0/url/?id={0}'.format(link1)},
   {"method":"GET", "relative_url": '/v3.0/url/?id={0}'.format(link2)}]

url = 'https://graph.facebook.com'
payload = json.dumps(batch)
headers = {'access_token : {0}'.format(fbtoken)}
response = requests.post(url, data=payload)
Robj = response.json()

print(Robj)

And here's the error:

{'error': {'message': 'Unsupported post request. Please read the Graph 
API documentation at https://developers.facebook.com/docs/graph-api', 
'type': 'GraphMethodException', 'code': 100, 'error_subcode': 33, 
'fbtrace_id': 'AcxF9FGKcV/'}}
1

There are 1 best solutions below

0
Jeff Bowen On

You need to pass the batch requests using the batch parameter like so:

payload = {'batch': json.dumps(batch)}