REST API 401 Error with API Token

Hello,

I’m trying to automate some functions for Confluence with the REST API in Python. However, all my requests return a 401 error:

import requests
import json

url = "https://[my site's url].atlassian.net/wiki/rest/api/analytics/content/[page key]/views"

headers = {
   "Authorization": "Bearer [API Token]"
}

response = requests.request(
   "GET",
   url,
   headers=headers
)

print(response.text)

The documentation keeps referring to Basic Authentication, but response I get from trying to use it is that it’s deprecated. However, the documentation does not have any mention of this, only this hidden page I found.

The examples for python REST requests has the format:

"Authorization": "Bearer <access_token>"

What am I supposed to put here? I’ve been using an API Token, the documentation for Marketplace refers to Access Tokens but for a completely different use.

Hi @AlecTremblay ,

Based on your code snippet, you might’ve confused API tokens with access tokens.

In the link you shared about the deprecation notice, the use of password is being deprecated in place of API tokens. That said, if you are keen on using basic authentication it should be

Authorization: Basic <your_encoded_string>

wherein the encoded string is your email to API token pair - your_email@domain.com:your_user_api_token. For more information, you can check this documentation.

If you prefer to create a 3LO app and use access tokens (based on the authorization header you used "Authorization": "Bearer [API Token]"), you can check out the guide here.

Lastly, to help you decide which authentication/security approach to use, you can check this page to determine which one best suits your use case. If you are building internal scripts to help automate some functions, then basic authentication might be sufficient.

Hope this helps.
Ian

1 Like

Thanks so much! That worked! Argh, I remember reading that portion of that article yesterday and moving on. Hours wasted!

We’re going to look into OAuth but we’ll be using basic to get started building. Thanks!

1 Like