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()
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)
Great, Thank you for sharing it here @Petar Dimovski
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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()
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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>"
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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).
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Ah ok, I am not so very good at python. If you try this using postman is that working ?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Please post your answer here if you have got it working.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.