使用Facebook Graph API发布评论时出错

时间:2018-05-21 14:36:28

标签: python facebook facebook-graph-api post django-rest-framework

我的目标是使用Facebook图表API发布对特定帖子ID的评论。

这是相同的代码段:

url = 'https://graph.facebook.com/v2.11/<post_id>/comments'
parameters = {'access_token': <FACEBOOK_ACCESS_TOKEN>, 'message': 'test comment'}
headers = {"content-type": "application/json"}
parameters = json.dumps(parameters)

response = requests.post(url, data=parameters, headers=headers, timeout=10)

我在DJANGO POST API中调用此API。

由于某些原因,通过此代码调用Facebook API不起作用。 API调用在10秒后超时。

如果我通过Postman / YARC调用Facebook API,则评论会成功发布。

任何人都可以告诉我哪里出错了吗?

1 个答案:

答案 0 :(得分:0)

Python请求示例:

导入请求

url =“https://graph.facebook.com/v2.11/yourPostId/comments

querystring = {“access_token”:“yourtoken”}

payload =“message = test%20comment” headers = {     'content-type':“application / x-www-form-urlencoded”,     'cache-control':“no-cache”     }

response = requests.request(“POST”,url,data = payload,headers = headers,params = querystring)

打印(response.text)

Python http.client示例:

导入http.client

conn = http.client.HTTPSConnection(“graph.facebook.com”)

payload =“message = test%20comment”

headers = {     'content-type':“application / x-www-form-urlencoded”,     'cache-control':“no-cache”     }

conn.request(“POST”,“/ v2.11 / yourPostId / comment?access_token = yourtoken”,有效负载,标题)

res = conn.getresponse() data = res.read()

打印(data.decode( “UTF-8”))