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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

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

 

2 answers

1 accepted

2 votes
Answer accepted

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.
Oct 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.
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
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
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
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
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
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
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
AUG Leaders

Atlassian Community Events