python - 使用 access_token 向 Facebook 的 Graph API 发出 POST 请求
问题描述
Facebook 关于为 Leadgen 创建测试线索的文档相当乏味。但是,它们提供了一些有用的 cURL 命令,并且似乎可以完成工作:
curl \
-F "access_token=<ACCESS_TOKEN>" \
"https://graph.facebook.com/<API_VERSION>/<FORM_ID>/test_leads"
和
curl \
-F "field_data=[{'name': 'favorite_color?', 'values': ['yellow']}, {'name': 'email', 'values': ['test@test.com']}]" \
-F "custom_disclaimer_responses=[{'checkbox_key': 'my_checkbox', 'is_checked': true}]" \
-F "access_token=<ACCESS_TOKEN>" \
"https://graph.facebook.com/<API_VERSION>/<FORM_ID>/test_leads"
就像我说的,这对我有用。但是,我想知道如何使用 Python 的requests
库及其post
方法提出这个请求。
这是我正在使用的代码:
token = "<MY_TOKEN"
url = "https://graph.facebook.com/<MY_API_VERSION>/<MY_FORM_ID>/test_leads"
r = requests.post(url, headers={'access_token': token})
我似乎无法通过使用 Python("code":100,"error_subcode":33
从 Facebook 返回)获得此请求,但使用 cURL 可以正常工作。我可以做些什么来让这个请求使用我的 Python 脚本工作。
编辑:结合我关于如何通过我的 Post 请求传递访问令牌的问题,我将如何传递他们在示例中显示的其他内容,嗯field_data
,和custom_disclaimer_responses
?
EDIT2:如果我使用 URL "https://graph.facebook.com/<MY_API>/<MY_FORM_ID>/test_leads?access_token="+token
,请求就会顺利通过。我似乎无法通过标题传递它。
解决方案
为了
curl \
-F "access_token=<ACCESS_TOKEN>" \
"https://graph.facebook.com/<API_VERSION>/<FORM_ID>/test_leads"
在python中你可以做
import requests
files = {
'access_token': (None, 'ACCESS_TOKEN'),
}
response = requests.post('https://graph.facebook.com/API_VERSION/FORM_ID/test_leads', files=files)
为了
curl \
-F "field_data=[{'name': 'favorite_color?', 'values': ['yellow']}, {'name': 'email', 'values': ['test@test.com']}]" \
-F "custom_disclaimer_responses=[{'checkbox_key': 'my_checkbox', 'is_checked': true}]" \
-F "access_token=<ACCESS_TOKEN>" \
"https://graph.facebook.com/<API_VERSION>/<FORM_ID>/test_leads"
在python中你可以做
import requests
files = {
'field_data': (None, '[ {'name': 'favorite_color?', 'values': ['yellow'] }, {'name': 'email', 'values': ['test@test.com'] } ]'),
'custom_disclaimer_responses': (None, '[ { 'checkbox_key': 'my_checkbox', 'is_checked': 'true' } ]'),
'access_token': (None, 'ACCESS_TOKEN'),
}
response = requests.post('https://graph.facebook.com/API_VERSION/FORM_ID/test_leads', files=files)
推荐阅读
- c# - “指示托管堆垃圾收集的开始”到底是什么意思,它是坏的吗?
- java - 如何在 Unix Makefile 中加入 Fortify Scan Analyzer 和 Apache Ant?
- java - 当从 UI 输入的值未持久化到数据库中时,如何调试 Java 应用程序?
- arrays - 如何检查数组数组中的边界重复项
- regex - Get-ADUser - 计算具有相同姓氏和电子邮件的用户(不包括数字)
- selenium - Selenium 如何访问嵌套元素(css BootStrap)
- python - pyinstaller 的 scipy 导入错误
- regex - 从单个日志行中提取多个字段数据实例到多值字段中
- excel - 用于在另存为对话框中将工作表另存为预命名文件的 VBA 代码
- configuration - 在akka中禁用协调关闭