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

incident_api.create_incident fails with Unauthorized, but same REST with Postman works

Matt Gerrans
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!
May 14, 2021

When posting with Postman, it works fine, but when I try to create an incident with the Python API, I get:

opsgenie_sdk.exceptions.AuthenticationException: You are not authorized to perform this action: (401)
Reason: Unauthorized: apiKey is invalid or integration is disabled

Here is a super simplified version of the python, opsgenie_api_key is just the guid without the 'GenieKey ' prefix:

def simple_create_incident(opsgenie_api_key, message='Alert', description='Description', priority='P5', asynchronous=False):
create_response, request_id = None, None
conf = opsgenie_sdk.configuration.Configuration()
conf.api_key['Authorization'] = opsgenie_api_key
api_client = opsgenie_sdk.api_client.ApiClient(configuration=conf)
incident_api = opsgenie_sdk.IncidentApi(api_client=api_client)
asc_time = time.asctime()
message = f'{message} @ {asc_time}'
body = opsgenie_sdk.CreateIncidentPayload(message=message, description=description, priority=priority)
if asynchronous:
thread = incident_api.create_incident(create_incident_payload=body, async_req=True)
create_response = thread.get()
else:
create_response = incident_api.create_incident(create_incident_payload=body)
request_id = incident_api.api_client.last_response.getheader('X-Request-ID')
return create_response or request_id

 

1 answer

0 votes
Justin Sitarz
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
June 9, 2021

Hi Matt!

 

The error response you're getting *usually* indicates that the API key you're using is incorrect or has been deleted or rotated in your Opsgenie account. Using your example code, I was able to set that up and successfully generate new incidents, when using an api key generated from the 'API Key Management' section of the app, granted with the 'Create and Update' rights. 

 

Could I have you generate a new API key in your account with those rights granted, and test it out once again? If you're still getting the same error, please let me know, and I we can take a closer look. 

 

Thanks!

 

Justin

Kartik Patra
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 19, 2023

Hi Justin,

 

I am facing the same issue as above. 

I have created a new API key with 'Create and Update' rights. Using this key iam getting the below exception.

opsgenie_sdk.exceptions.AuthenticationException: You are not authorized to perform this action: (403)

 

Can you please help me with this?

 

Thanks,

Kartik

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
TAGS
AUG Leaders

Atlassian Community Events