使用 x-api-key 从 cm commerce 中提取数据
Pull data from cm commerce using x-api-key
我正在尝试通过引用此 guide. 来提取数据
我有点新。我可以只使用 api 键和 url 提取数据吗?因为我只有 api 键和 url。我没有任何其他参数。以下是我尝试过的方法:
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)
还有这个:
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)
但我有错误:
AttributeError: 'Response' object has no attribute 'type'
在其他方法中,我得到 401 错误
python-requests
一个人就可以为你做这件事(不需要 urllib
)。
你有 API key
所以你不应该使用 HTTPBasicAuth
这应该适合你:
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())
我正在尝试通过引用此 guide. 来提取数据 我有点新。我可以只使用 api 键和 url 提取数据吗?因为我只有 api 键和 url。我没有任何其他参数。以下是我尝试过的方法:
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)
还有这个:
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)
但我有错误:
AttributeError: 'Response' object has no attribute 'type'
在其他方法中,我得到 401 错误
python-requests
一个人就可以为你做这件事(不需要 urllib
)。
你有 API key
所以你不应该使用 HTTPBasicAuth
这应该适合你:
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())