如何将 curl 转换为 python 请求

How to convert curl to 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 时,我得到了正确的响应。

我做错了什么?

您确定要提出 POST 请求吗?因为 -G flag 表示发出 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()