Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Atlassian API Token doesn't work in python script but it works in curl

Petar Dimovski October 27, 2021

Hello Everyone, 

I created an access token in the atlassian web application and it works when I use curl like this:

curl --user "petar.dimovski@etxcapital.com:TOKEN https://etxcapital.atlassian.net/wiki/rest/api/content&type=page

but when I ping the api from a python script I get the following response:

{"message":"Current user not permitted to use Confluence","statusCode":403}

What should I do in order to make it work like curl does?

Here's the python script:

import os
import requests
from requests.auth import HTTPBasicAuth

confluence_api_url ="https://etxcapital.atlassian.net/wiki/rest/api"

def get_auth_details():
return HTTPBasicAuth(os.getenv("username"), os.getenv('token'))


def get_pages(label=None):
params = {
'type':'page',
# 'start':0,
# 'limit':500
}
if label:
params["label"] = label

resp = requests.get(f"{confluence_api_url}/content/search.json",
auth=get_auth_details(),
params=params)

return resp.json()

 

2 answers

1 accepted

2 votes
Answer accepted
Petar Dimovski October 28, 2022

I just used the Confluence python API from the atlassian library. The password should be an API token.

from atlassian import  Confluence
username = CONFLUENCE_VIEWER_USERNAME # if role == role.VIEWER else CONFLUENCE_ADMIN_USERNAME
token = CONFLUENCE_VIEWER_PASSWORD # if role == role.VIEWER else CONFLUENCE_ADMIN_PASSWORD
return Confluence(
url=CONFLUENCE_HOST_URL,
username=username,
password=token,
cloud=True)

Kishan Sharma
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 28, 2022

Great, Thank you for sharing it here @Petar Dimovski 

0 votes
Kishan Sharma
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 31, 2021

Hi @Petar Dimovski Welcome to the Atlassian Community!

You'll need to encode your authorization credentials to base64 which is important step, you can refer it here on how to do it. Also take a look at sample script on this link

Let me know if that help!

Petar Dimovski November 1, 2021

Hi @Kishan Sharma , Thank you for taking your time to help me.

After encoding my credentials I get the following error as a response:

Basic authentication with passwords is deprecated. For more information, see: https://confluence.atlassian.com/cloud/deprecation-of-basic-authentication-with-passwords-for-jira-and-confluence-apis-972355348.html 

My username is the email and token is the API Token.

Here is the relevant part of the code:

def get_auth_details():
print(os.getenv('username'))
s=f'{os.getenv("username")}:{os.getenv("token")}'
encoded=base64.b64encode(s.encode())
print(s)
print(f"Encoded:{encoded}")
print(f"Decoded:{base64.b64decode(encoded)}")
return f"Basic {encoded}"


def get_pages(label=None):
params = {
'cql':'type=page',
}
if label:
params["cql"] += f" and label={label}"

resp = requests.get(f"{confluence_api_url}/content/search.json",
headers={'Authorization':get_auth_details(),'Content-Type': 'application/json'},
params=params)

return resp.json()
Kishan Sharma
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 2, 2021

Hi @Petar Dimovski 

You need to Supply an Authorization header with content Basic followed by the encoded string.

 Example: 

Authorization: Basic eW91cl9lbWFpbEBkb21haW4uY29tOnlvdXJfdXNlcl9hcGlfdG9rZW4=

 

ie - "Authorization: Basic <your_encoded_string>"

Petar Dimovski November 2, 2021

As you can see in the code, in the headers parameter I have a dictionary which gives the Authorization header of Basic "encoded string" (that is what get auth details returns).

Kishan Sharma
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 2, 2021

Ah ok, I am not so very good at python. If you try this using postman is that working ?

Kishan Sharma
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 7, 2021

Please post your answer here if you have got it working.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events