Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Getting error 401 on REST API (Python)

Hi!

I'm trying to create a new page via REST API and Python.

But I'm getting error 401 'Request rejected because issuer is either not authorized or not authorized to impersonate'.

Here's my code:

import requests
import json
from requests.auth import HTTPBasicAuth

url = 'https://<my_domain>.atlassian.net/wiki/rest/api/content'
auth = HTTPBasicAuth(<my_google_email>, <my_api_token>)

headers = {
'Authorization': 'Bearer {}'.format(<my_api_token>),
'Content-Type': 'application/json',
'Accept': 'application/json',


}

data = {
'type': 'page',
'title': 'My_New_Page',
'ancestors': [{'id': 393222}],
'space': {'key': 'BDPP'},
'body': {
'storage': {
'value': 'content',
'representation': 'storage',
}
}
}

r = requests.post(url=url, data=json.dumps(data), headers=headers, auth=auth)
# Consider any status other than 2xx an error
if not r.status_code // 100 == 2:
print("Error: Unexpected response {}".format(r.text))
else:
print('Page Created: {}'.format(r))

And here https://id.atlassian.com/manage-profile/security/api-tokens I see that the token has been used.

 Am I doing something wrong?

1 answer

1 accepted

Hi

Have you tried the Authorization header with Basic authentication?

Authorization: Basic <Base64 encoded username and password>

Base64 encode the string of you jira user and api token separated by a colon

If I try Authorization header with Basic authentication like this:

'Authorization': 'Basic {}'.format(base64.b64encode(b'<my_google_email>:<my_api_token>'))

 I get error 401 'Basic authentication with passwords is deprecated.'

Hi 

I have tried the exact same code and able to create a page with a response

Page Created: <Response [200]>

I also removed the Authorization header from headers

headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
}

and left it using the auth in the post request

auth=auth 
auth = HTTPBasicAuth(<my_google_email>, <my_api_token>)

 and it created ap age.

 

I would check you the API key that is being used 

Thanks for your tip, I just noticed a typo in my email address.

I tried my code and yours and both worked correctly.

Thanks for your help!

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
FREE
PERMISSIONS LEVEL
Site Admin
TAGS
Community showcase
Published in Confluence Cloud

🏠 Say hello to the new Confluence Home!

Hi Atlassian Community, My name is DJ Chung, and I’m a Product Manager on the Confluence Cloud team. Today, I’m excited to share a new and improved version of Home. The new Home helps you ...

40,882 views 28 130
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you