python - 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/'}}
解决方案
您需要使用batch
参数传递批处理请求,如下所示:
payload = {'batch': json.dumps(batch)}
推荐阅读
- laravel - 错误:未定义的变量:视图中的数据(foreach)
- python - 在第一次请求/解析之前在蜘蛛中抓取返回项目
- flutter - 如何删除TextField下方的下划线?
- c - 在一种方法中使用两个信号量
- java - 为什么我的字符串比较代码不起作用?
- javascript - 尝试使 while 循环的范围大于 10 时出错
- logstash - Logstash 多行输入适用于本地但不适用于 EC2 实例
- javascript - 在 reducer 中状态更改后,组件不会重新渲染
- python-3.x - 移动文件夹中所有文件的逻辑错误
- java - 处理给定输入文件和我的查询文件中常见的单词