首页 > 解决方案 > Graph API 的简单批处理请求返回 Unsupported Post Request 错误

问题描述

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

但是当我运行我的代码(见下文)时,我收到了 Unsupported 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/'}}

标签: pythonfacebookfacebook-graph-apipython-requestsfacebook-batch-request

解决方案


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

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


推荐阅读