Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,299,579
Community Members
 
Community Events
165
Community Groups

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

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()

 

1 answer

0 votes
Kishan Sharma Community Leader Oct 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!

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 Nov 02, 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>"

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 Nov 02, 2021

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

Kishan Sharma Community Leader Nov 07, 2021

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

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Atlassian Access

Atlassian Access Demo Q&A Recap

Hi Community! Thank you to all who joined our ongoing monthly Atlassian Access demo! We have an engaging group of attendees who asked many great questions. I’ll share a recap of frequently ask...

1,160 views 4 4
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you