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

How to connect to Atlassian python API using Auth 2.0 Bearer Token

equigley2 January 5, 2023

Hi - I am trying to connect to Confluence with the Atlassian python API using Oauth 2.0 with a bearer token as required by my company because we have our own domain and use multi-factor authentication. 

The link below shows the different authentication methods. 
https://atlassian-python-api.readthedocs.io/index.html

I have tested my token and URL using the below on the command line and it works (token and content shown below are fake)
curl -H "Authorization: Bearer XYMWODLW" https://wiki.mycompany.com/rest/api/content/123

I am having trouble translating this to one of the authentication methods in the link. Bearer tokens are typical in Oauth 2.0. When I reference the Oauth 2.0 section, I get the below code.

from atlassian.bitbucket import Cloud

# token is a dictionary and must at least contain "access_token"
# and "token_type".
oauth2_dict = {
    "client_id": client_id,
    "token": token}

bitbucket_cloud = Cloud(
    oauth2=oauth2_dict)

# For a detailed example see bitbucket_oauth2.py in
# examples/bitbucket

My questions: 
1. Does this also work for Confluence?
2. Where do I get client_id?
3. What is the actual format? Example shown below. (I was unable to find bitbucket_oauth2.py to reference)

oauth2_dict = { "client_id": client_id,

                          "token": {"access_token" : "XYMWODLW",

                                         "token_type" : "Bearer"

                                         }

                        }

1 answer

5 votes
equigley2 January 5, 2023

Hi, I have solved my above issue and wanted to report back the solution in case it helps anyone else.

import requests
from atlassian import Confluence

s = requests.Session()
s.headers['Authorization'] = 'Bearer XYMWODLW'
confluence = Confluence(url='https://wiki.mycompany.com/', session=s)

Then you can use the methods in the python API wrapper
https://atlassian-python-api.readthedocs.io/index.html
confluence.get_page_space('123')

(Fake bearer token above)

Reference:

https://github.com/atlassian-api/atlassian-python-api/issues/467

greenglass April 11, 2023

Hi @equigley2 , 
I have the same issue. I get the error: HTTPError: Current user not permitted to use Confluence

 

To get the Bearer Token, did you simply follow the documentation ?

 

I also checked the scopes in the Developer Console and added as much as possible without success. Any insights are more than welcome.

 

Thanks in advance

Yasir Kusay October 5, 2023

Thanks @equigley2

I spent some time trying to get this to work but your solution worked for me.

Ishan Kaul
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
April 4, 2024

Hi , @equigley2 @Yasir Kusay I am attempting what I think is the same code, but still getting an HTTPError. My code looks like

s = Session()
s.headers["Authorization"] = f"Bearer {access_token}"
confluence = Confluence(url=base_url, session=s)
page_contents = confluence.get_page_by_id(page_id)

I get my access_token from the oauth 2.0 handshake with confluence, and base url through another request to the atlassian accessible-resources api

The scopes returned from accessible resources from the token looked like:


#readonly:content.attachment:confluence

#read:confluence-content.all
#read:confluence-content.summary
#read:confluence-user
#search:confluence
#read:confluence-space.summary
so I don't think it's a permissions issue.


Is there anything you guys did differently to get it working?

Ishan Kaul
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
April 4, 2024

UPDATE: I have found the solution finally. Since Atlassian's python client library now points to the v2 API, the url passed to Confluence.__init__ needs to be structured like  

(https)"api.atlassian.com/ex/confluence/{client_id}/wiki/" (NOTE: the url provided in the python example for the v2 docs is incorrect as of this writing). The python client will inject "
rest/api/content/{page_id}" into the path when it calls get_page_by_id.
So for anyone trying to do this by making a request, the final url in the GET request is 
api.atlassian.com/ex/confluence/{client_id}/wiki/rest/api/content/{page_id}.

Note that I also went on a mad frenzy adding both classic and granular read scopes so I'm not sure which ones actually allowed it to work

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events