Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

I am getting HTTP error occurred: 403 Client Error: Forbidden for url for the below

Sudarsana G
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 21, 2024

import csv
import requests
# Replace these with your actual credentials and API endpoint
API_TOKEN = 'API-Toekn'
API_URL = 'https://mysite.atlassian.net/wiki/rest/api/space'
HEADERS = {
'Authorization': f'Bearer {API_TOKEN}',
'Content-Type': 'application/json'
}
def get_spaces():
spaces = []
next_page = API_URL

try:
while next_page:
response = requests.get(next_page, headers=HEADERS)
response.raise_for_status() # Raise an exception for HTTP errors
data = response.json()

# Check if 'results' key exists in the response
if 'results' in data:
for space in data['results']:
spaces.append({
'key': space['key'],
'name': space['name'],
'total_pages': 0
})

next_page = data.get('next')
else:
# If 'results' key is not found, print the response for debugging
print("No 'results' key found in response:")
print(data)
# Break the loop to avoid infinite loop
break

except requests.exceptions.HTTPError as e:
print(f"HTTP error occurred: {e}")
# Return an empty list of spaces to indicate failure
return []

except Exception as e:
print(f"An error occurred: {e}")
# Return None to indicate failure
return None

return spaces

def get_space_pages(space_key):
total_pages = 0
next_page = f'{API_URL}/{space_key}/content'

while next_page:
response = requests.get(next_page, headers=HEADERS)
data = response.json()

# Decide which pages to count (e.g., pages with a certain label)
for page in data['results']:
# Check if 'desired_label' is not numeric
if not page['metadata'].get('desired_label', '').isdigit():
# Increment total_pages
total_pages += 1

next_page = data.get('next')

return total_pages

def main():
spaces = get_spaces()

if spaces is None:
print("Failed to retrieve spaces. Exiting...")
return

for space in spaces:
space['total_pages'] = get_space_pages(space['key'])
print(f"Space Key: {space['key']}, Space Name: {space['name']}, TotalPagesPerSpace: {space['total_pages']}")

if __name__ == '__main__':
main()

I have a site admin permission can any one please help

1 answer

0 votes
Shawn Doyle - ReleaseTEAM
Community Champion
April 22, 2024

Typo in the API Token?

 

API_TOKEN = 'API-Toekn'

'Authorization': f'Bearer {API_TOKEN}',

 

Sudarsana G
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 29, 2024

Not really but i was able to fix it. instead f'Bearer {API_TOKEN}', i have used f'basic {API_TOKEN}', Thank you for your reply

Suggest an answer

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

Atlassian Community Events