Graph API的简单批处理请求返回不支持的Post Request错误

时间:2018-07-03 07:34:05

标签: python facebook facebook-graph-api python-requests facebook-batch-request

我正在尝试通过Graph API获取公众参与度指标,以获取链接列表。由于它们很多,因此需要批量请求才能避免达到速率限制。使用Facebook提供的engagement endpoint for linksbatch API guide,我将批处理请求格式化为词典列表,并通过POST而不是get提交。

但是,当我运行我的代码时(见下文),我收到不支持的Post Request错误。

我落后并且精疲力尽,我们将不胜感激。

这是我的代码:

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)

这是错误:

{'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 个答案:

答案 0 :(得分:0)

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

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