Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,366,304
Community Members
 
Community Events
168
Community Groups

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

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

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

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
TAGS

Atlassian Community Events