Python 使用访问令牌向Facebook发出POST请求';s图形API

Python 使用访问令牌向Facebook发出POST请求';s图形API,python,python-3.x,python-2.7,facebook-graph-api,Python,Python 3.x,Python 2.7,Facebook Graph Api,Facebook关于为leadgen创建测试线索的文档相当平淡。但是,它们提供了一些有用的cURL命令,似乎可以完成任务: curl \ -F "access_token=<ACCESS_TOKEN>" \ "https://graph.facebook.com/<API_VERSION>/<FORM_ID>/test_leads" 我似乎无法通过使用Python获得此请求(从Facebook返回“code”:100,“error\u subcode”:33

Facebook关于为leadgen创建测试线索的文档相当平淡。但是,它们提供了一些有用的cURL命令,似乎可以完成任务:

curl \
-F "access_token=<ACCESS_TOKEN>" \
"https://graph.facebook.com/<API_VERSION>/<FORM_ID>/test_leads"
我似乎无法通过使用Python获得此请求(从Facebook返回
“code”:100,“error\u subcode”:33
),但使用cURL就可以很好地工作。如何使用Python脚本使此请求正常工作

编辑:结合我关于如何通过Post请求传递访问令牌的问题,我将如何传递示例中显示的其他内容,例如,
字段\数据
,以及
自定义\免责声明\响应

编辑2:如果我使用URL
“https://graph.facebook.com///test_leads?access_token=“+token
请求将轻松通过。我似乎无法通过标题传递它

curl \
-F "access_token=<ACCESS_TOKEN>" \
"https://graph.facebook.com/<API_VERSION>/<FORM_ID>/test_leads"
为了


我不知道这是否是你的问题,但你错过了一个大括号。r=requests.post(url,headers={'access_token':token})@DouglasPlumley谢谢。实际上,我的代码中有一个大括号。我在复制粘贴上搞砸了…“我似乎无法通过标题传递它。”-谁说你应该
-F
来自原始cURL命令,表示简单的POST参数,而不是标题。我认为Graph API根本不会将访问令牌作为请求头。
token = "<MY_TOKEN"
url = "https://graph.facebook.com/<MY_API_VERSION>/<MY_FORM_ID>/test_leads"

r = requests.post(url, headers={'access_token': token})
curl \
-F "access_token=<ACCESS_TOKEN>" \
"https://graph.facebook.com/<API_VERSION>/<FORM_ID>/test_leads"
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"
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)