Simple Checkvist API authentication in Python

I regret displaying my ignorance of basic Python and HTTP authentication here.

I have the following in a Google Colab cell. I know that both password and API key are not required, but I was experimenting with every combination of both without success.

I get the expected results from the public list, but not from my private list. I bomb out with “Private failed”.

I believe that I’m using the URL corresponding to retrieving a token, but not accepting and passing the token on subsequent calls. For my simple-minded purposes, I’d be happiest with basic HTTP authentication in the headers.

USERNAME = "charlie@hamufamu.org"
PASSWORD = "what I type to log in" # @param {type:"string"}
REMOTE_KEY = "what I see on the profile page" # @param {type:"string"}

import requests

session = requests.Session()

# This "works" even with the wrong credentials
response = session.post(
    "https://checkvist.com/auth/login?version=2",
    data={"username": USERNAME, "password": PASSWORD, "remote_key": REMOTE_KEY})
if response.status_code != 200:
  raise SystemExit("Authentication failed")

# Access a public list
response = session.get("https://checkvist.com/checklists/485490.json")
if response.status_code != 200:
  raise SystemExit("Public failed")
print(response.content)

# Get my private list
response = session.get("https://checkvist.com/checklists/464277.json")
if response.status_code != 200:
  raise SystemExit("Private failed")
print(response.content)

# We made it to the end
print("Success!")

I’ve resolved my problem. Here’s the approach with basic HTTP authentication:

import requests
from google.colab import userdata

# Replace 'username' and 'password' with your actual credentials
username = userdata.get('username')
password = userdata.get('password')

# Base URL for the Checkvist API
base_url = 'https://checkvist.com/checklists.json'

# Send a GET request to retrieve the list of checklists
response = requests.get(base_url, auth=(username, password))

# Check if the request was successful
if response.status_code == 200:
    # Print the list of checklists
    checklists = response.json()
    for checklist in checklists:
        print(checklist['name'])
else:
    print(f"Failed to retrieve checklists: {response.status_code} - {response.text}")
1 Like

If you don’t mind saying, what is the goal of your scripting? Thanks!

Two purposes:

  1. Playing around and learning. Finally forced myself to learn basic HTTP authentication in Python, as seen above.

  2. Building myself a little utility to take all my overdue items and to scatter them to random dates in the future. (It may not be quite as silly as it sounds. Or it may be.)

Thanks, ChatGPT!

(The original, incorrect, answers I received were also from ChatGPT. But once I learned the right terms to ask for, I coaxed it into the right answer.)

For the benefit of other newbies, basic HTTP authorization needs to be provided on each call. (I assume that sessions solve this problem, but I haven’t gotten there yet.)

import requests
from google.colab import userdata
auth = (userdata.get('username'), userdata.get('password'))

# This works and lists the name of each checklist in my account.
response = requests.get('https://checkvist.com/checklists.json', auth=auth)
if response.status_code != 200:
  raise SystemExit(f"Failed to retrieve checklists the first time:" 
                   + " {response.status_code} - {response.text}")
for checklist in response.json():
  print(checklist['name'])

# This will fail because authorization needs to be provided each time.
response = requests.get('https://checkvist.com/checklists.json')
if response.status_code != 200:
  raise SystemExit(f"Failed to retrieve checklists the second time:" 
                   + " {response.status_code} - {response.text}")
for checklist in response.json():
  print(checklist['name'])

So a minimal Hello World could be:

import requests
response = requests.get('https://checkvist.com/checklists.json',
                        auth=('YOUR-USERNAME','YOUR-PASSWORD'))
for checklist in response.json():
  print(checklist['name'])

From a learning perspective, the next steps for me would be:

  1. Can I use a session to avoid repeated authentication?
  2. Can I use a token like a modern person?

I’ll work to figure out on my own, but I wouldn’t mind spoilers.

From a personal utility perspective, the next steps for me would be:

  1. Collecting a list of overdue tasks.
  2. Pushing their due dates off into the future.

Here using a session saves sending the authentication with each request. No easier or harder for me as a programmer. I assume a little more efficient?

from google.colab import userdata
USERNAME = userdata.get('username') # 'user@domain.com`
PASSWORD = userdata.get('password') # 'yourPassword123'

import requests
session = requests.Session()

response = session.get('https://checkvist.com/checklists.json',
                        auth=(USERNAME,PASSWORD))
for checklist in response.json():
  print(checklist['name'])

response = session.get('https://checkvist.com/checklists.json')
for checklist in response.json():
  print(checklist['name'])

Hello,

I just wanted to mention that there is a Python script which handles token-based authentication in Checkvist - GitHub - ChewingPencils/checkvist-python: Python Wrapper for the Checkvist API . Maybe it will be interesting for you to take a look at it :slight_smile:

Kind regards,
KIR