简体   繁体   中英

Simple batch request for Graph API returning Unsupported Post Request error

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/'}}

您需要使用batch参数传递批处理请求,如下所示:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM