简体   繁体   中英

How to convert curl to python requests

I'm using facebook api to create ads.

I have next curl:

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

And I want to use it with python requests library. I try as follows:

data = {
   'access_token': user_token
}

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

But when I execute it I get an error:

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

But when I execute the curl I get correct response.

What am I doing wrong?

Are you sure you need to make a POST request? Because -G flag means make a GET request.

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM