【问题标题】:Simple batch request for Graph API returning Unsupported Post Request errorGraph API 的简单批处理请求返回 Unsupported Post Request 错误
【发布时间】:2018-12-11 10:26:06
【问题描述】:

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

但是当我运行我的代码时(见下文),我收到了一个不支持的发布请求错误。

我落后了,筋疲力尽,任何帮助将不胜感激。

这是我的代码:

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

【问题讨论】:

  • “由于数量很多,因此需要批量请求以避免设置速率限制。” - 继续做梦……developers.facebook.com/docs/graph-api/advanced/…“你也可以使用 Batch API 对您的请求进行批处理,但请注意,每个子请求都是其自己的 API 调用 [...]” 您的上述批处理将被视为 两个 API 调用,因此您关于速率限制,这里没有保存任何内容。
  • 虽然节省了 HTTP 往返时间
  • 试试requests.post(url, json=batch)
  • 我做了,但很遗憾,它没有用。 :(

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


【解决方案1】:

您需要像这样使用batch 参数传递批处理请求:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多