carbon.txt - shared secret demo

First of all, install requests so we can easily make http requests with python

!pip install requests
3.6s

For accessing these endpoints, if I am not already signed in, I'll need to authenticate myself.

The example below shows us using basic auth, with the well known requests Python with Python. See the docs on basic auth for more details:

import os
import requests
from requests.auth import HTTPBasicAuth
basic_password = os.getenv("BASIC_AUTH_PASSWORD")
basic = HTTPBasicAuth('mrchrisadams_staff', basic_password)
response = requests.post(
  "http://admin.thegreenwebfoundation.org/api/v3/carbontxt_shared_secret"
  , auth=basic
)
# return the response so we can see our status code at least
response
0.6s
import json
json_content = response.json()
with open("/results/response-as-pretty.json", "w") as jsonfile:
  jsonfile.write(json.dumps(json_content))
0.0s

With a secret set, I can now easily fetch the same secret, so I can make the domain hashes I need to:

get_response = requests.get(
  "http://admin.thegreenwebfoundation.org/api/v3/carbontxt_shared_secret"
  , auth=basic
)
response.json()
0.6s

Runtimes (1)