Python API call failed with status 404 and 401 - Confluence Cloud

Christoph Schaub March 24, 2023

Hi 

I think I tried all the ressources and helps and just don't find a solution.

Before doing any other thing I want just to check if the combination of User-ID and Token is working correct for the Confluence Cloud. 

The Id I use is from an Admin and the Token shows that it was used and is generated with that admin account. However, the Python program does always fail. 

Does anyone have a good idea what else I could modify ? The code currently looks like this:

 

import requests

BASE_URL = "https://companyname.atlassian.net"
SPACE_URL = f"{BASE_URL}/wiki/spaces/{{space_key}}/overview"
USER_ID = "my.name@companyname.com"
ACCESS_TOKEN = "TokenTokenTokenTokenTokenTokenTokenTokenTokenTokenTokenTokenTokenTokenTokenToken"
AUTH = f"{USER_ID}:{ACCESS_TOKEN}"
API_URL = f"{BASE_URL}/wiki/rest/api"


def check_api_credentials():
response = requests.get(API_URL, auth=(USER_ID, ACCESS_TOKEN))
if response.status_code == 200:
print("API credentials are valid!")
elif response.status_code == 401:
print("API call failed with status code 401. Check your API credentials.")
else:
print(f"API call failed with status code {response.status_code}")


def list_groups_with_authorizations(space_key):
space_url = SPACE_URL.format(space_key=space_key)
response = requests.get(space_url, headers={"Authorization": f"Basic {AUTH}"})
if response.status_code == 200:
data = response.json()
groups = data["groups"]
print(f"Groups with authorizations in space {space_key}:")
for group in groups:
print(group["name"])
elif response.status_code == 401:
print("API call failed with status code 401. Check your API token and permissions.")
else:
print(f"API call failed with status code {response.status_code}")


check_api_credentials()
list_groups_with_authorizations("OneOfTheSpacesName")

 

 

0 answers

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
PREMIUM
PERMISSIONS LEVEL
Site Admin
TAGS
AUG Leaders

Atlassian Community Events