如何将curl转换为python请求

时间:2019-07-17 06:28:09

标签: python curl facebook-graph-api python-requests

我正在使用Facebook api来制作广告。

下一个卷曲:

curl -G \
-d "access_token=<ACCESS_TOKEN>" \
https://graph.facebook.com/<API_VERSION>/<PRODUCT_CATALOG_ID>/product_sets

我想将其与python requests库一起使用。我尝试如下:

data = {
   'access_token': user_token
}

response = requests.post('https://graph.facebook.com/v3.3/{0}/product_sets'.format(catalog_id), data=data)

但是当我执行它时,我得到一个错误:

{'error': {'message': '(#100) The parameter name is required', 'type': 'OAuthException', 'code': 100, 'fbtrace_id': 'AcUg6UZivr_rNWgkwdEHaZl'}}

但是当我执行curl时,我会得到正确的响应。

我在做什么错了?

1 个答案:

答案 0 :(得分:1)

您确定需要提出POST请求吗?因为-G标志意味着发出GET请求。

https://curl.haxx.se/docs/manpage.html#-G

url = 'https://graph.facebook.com/{API_VERSION}/{PRODUCT_CATALOG_ID}/product_sets'.format(
    API_VERSION='v3.3',
    PRODUCT_CATALOG_ID='catalog_id123'
)
res = requests.get(url, params={
    'access_token': 'my access token'
})
res.raise_for_status()
data = res.json()