Pull data from cm commerce using x-api-key

476 Views Asked by At

i am trying to pull data by referencing this guide. I'm a little new. can i just pull data with api key and url. Because i have only api key and url. I don't have any of the other parameters. Here are the ways i tried:

import urllib.parse
import urllib.request

url = "https://commerce.campaignmonitor.com/api/v1/abandoned-carts/campaigns"
header={"x-api-key" : 'my_api_key'}
post_param = urllib.parse.urlencode({
                    'user' : 'i_dont_know',
           'status-update' : 'i_dont_know'
          }).encode('UTF-8')

req = urllib.request.Request(url, post_param, header)
response = urllib.request.urlopen(req)

and this:

from requests.auth import HTTPBasicAuth
import requests
import urllib

url ="https://commerce.campaignmonitor.com/api/v1/abandoned-carts/campaigns"
headers = {"Accept": "application/json"}
auth = HTTPBasicAuth('my_api_key', 'i_dont_know')

req = requests.get(url, headers=headers , auth=auth)
response = urllib.request.urlopen(req)

but i have error:

AttributeError: 'Response' object has no attribute 'type'

in other methods, I get 401 error

1

There are 1 best solutions below

1
Tarique On BEST ANSWER

python-requests alone can do this for you (no need for urllib). You have the API key so you shouldn't use the HTTPBasicAuth

this should work for you:

import requests

url ="https://commerce.campaignmonitor.com/api/v1/abandoned-carts/campaigns"

# like the doc says, provide API key in header
headers = {"Accept": "application/json",
'X-ApiKey': 'my_api_key'}

req = requests.get(url, headers=headers)
print(req.json())