How to connect to Atlassian python API using Auth 2.0 Bearer Token
Asked by equigley2:

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](https://api.wikistg.autodesk.com/rest/api/content/1462698784)  

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"

}

}


Answers:
author: equigley2
body: 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>

comments: author: R. Angel
body: Hi [@equigley2](/t5/user/viewprofilepage/user-id/5114530) ,   
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](https://developer.atlassian.com/cloud/jira/platform/oauth-2-3lo-apps/) ?

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


author: Yasir Kusay
body: Thanks [@equigley2](/t5/user/viewprofilepage/user-id/5114530)

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


author: Ishan Kaul
body: Hi , [@equigley2](/t5/user/viewprofilepage/user-id/5114530) [@Yasir Kusay](/t5/user/viewprofilepage/user-id/5333120) 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:

<br />

#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.

<br />

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


author: Ishan Kaul
body: 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](https://developer.atlassian.com/cloud/confluence/rest/v2/api-group-page/#api-pages-id-get) 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


Tags: api

confluence-cloud

oauth

python

server