Basic API authentication question - Campaign Monitor

486 Views Asked by At

Hi Stackoverflow community! I'm just learning how to use APIs and i'm struggling with authentication. I'm trying to use the Campaign Monitor api to view some data and potentially create/delete stuff in my playground sandbox. I'm running into this authentication error.

<Response [401]> {"Code":50,"Message":"Must supply a valid HTTP Basic Authorization header"}

I see that I don't have the API key listed anywhere in the get_segment get request, i've tried to append the API key as a parameter in the API url like this "segments.json?&api_key={API_KEY}" but that gives me the same error. Nowhere in the Campaign Monitor documentation do they explain how to use the key and it's driving me crazy. Any thoughts and suggestions would be greatly appreciated!

import requests

API_KEY = '12345'
LIST_ID = '123ABc'

API_Get_List_Segments = f'https://api.createsend.com/api/v3.3/lists/{LIST_ID}/segments.json?'

def get_segment_function():
    get_segment = requests.get(url = API_Get_List_Segments)
    print(get_segment)
    print(get_segment.text)

get_segment_function()
1

There are 1 best solutions below

0
On

Ok I figured it out! *note that i spent like 4 hours on this before posting the question.

the CM documentation is not very clear about things but they give one example using curl. I have no clue what curl is but i googled curl to python and found a translator that showed me how to enter the authentication.

So in the request you add the authentication parameter in this way

auth=(API_KEY,'x'))

So the request would look like this

get_segment = requests.get(url = API_Get_List_Segments, auth=(API_KEY,'x'))

Blockquote