API Token not working with REST calls (basic auth deprecated)
Asked by paul.bruillard:

I'm trying to access the confluence REST API from Python for creating and updating pages. I followed the following instructions to generate an API key since basic auth for passwords was deprecated:

* <https://developer.atlassian.com/cloud/confluence/basic-auth-for-rest-apis/>
* <https://support.atlassian.com/atlassian-account/docs/manage-api-tokens-for-your-atlassian-account/>

I've since tried accessing the Confluence API with my key using several methods:

* Postman
* curl
* Python requests library
* Python confluence module

All of the options return (or similar error if using the Atlassian Python module)

```
401: Basic authentication with passwords is deprecated.  For more information, see: https://developer.atlassian.com/cloud/confluence/deprecation-notice-basic-auth/
```

For reference here are the Python and curl scripts. Below the username is my email and the api token was obtained through the confluence dashboards as outlined above. I've tried two different API tokens and both behave the same.

My company authenticates through Okta, is there some additional step I need to take to enable programmatic access?

**Requests Code:**

```
import requests
from requests.auth import HTTPBasicAuth

username: str = 'my_email'
token: str = 'my_api_key'
url: str = 'https://<my_company_domain>.atlassian.net/status'

auth = HTTPBasicAuth(username,token)
r = requests.get(url=url,auth=auth)

print(f'{r.status_code}: {r.content}')
```

This returns

```
401: Basic authentication with passwords is deprecated. For more information, see: https://developer.atlassian.com/cloud/confluence/deprecation-notice-basic-auth/
```

**Confluence Module Code**

```
from atlassian import Confluence

username: str = 'my_email'
token: str = 'my_api_key'
url: str = 'https://<my_company_domain>.atlassian.net/wiki'

confluence = Confluence(
url = url,
username = username,
token = token,
cloud=True
)

confluence.get_user_details_by_username(username)
```

This returns

```
HTTPError: Request rejected because issuer is either not authorized or not authorized to impersonate
```

**cURL:**

```
curl -v https://<my_company_domain>.atlassian.net/status --user <username>:<api_token>
```

This returns

```
Basic authentication with passwords is deprecated. For more information, see: https://developer.atlassian.com/cloud/confluence/deprecation-notice-basic-auth/
```


Answers:
author: Oliver Siebenmarck _Polymetis Apps_
body: Hi [@paul.bruillard](/t5/user/viewprofilepage/user-id/5143376) ,

From what I can tell by looking at your examples, you are not doing anything wrong or differently then I typically do.

Which leads me to believe, that maybe it has something to do with the central authentication. There might a global setting or policy that forbids API access, while I have not seen or used anything like this so far, I've heard about a few times. Might definitely be worth to reach out to your org admin and see if they have anything set up.

Hope that helps,  
Oliver


author: Mohamed Benziane
body: Hi,

Welcome to the community

With python if you want to use the API token you can try this :

```
import requests

token = "API Token"
response = requests.get("https://<yourUrl>/rest/api/2/",headers={'Authorization': "Bearer " + token})
```

comments: author: paul.bruillard
body: The server response is just a redirect to the login page.


Tags: api

authentication

cloud

confluence-cloud

python

rest

rest-api

sso