Issue creating a webhook in Jira using Python and OAuth 2.0 token(Jira Webhooks)

Satyajit
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!
November 5, 2023


I'm attempting to create a webhook in Jira using Python and an OAuth 2.0 token for authentication. However, I'm encountering an error message, and I could use some guidance on how to resolve it.

Here's the Python code I'm using:


import requests
import json

def create_jira_webhook(project_key, oauth_token):
# Define the URL for creating a webhook in Jira
webhook_url = f"https://enviatetesting.atlassian.net/rest/api/3/webhook"

# Define the webhook payload
webhook_payload = {
"name": "Your Webhook Name",
"url": webhook_url,
"events": ["jira:issue_created", "jira:issue_updated", "jira:issue_deleted"],
"properties": {
"jqlFilter": f"project={project_key}"
      }
}

# Defining the headers with OAuth 2.0 token
headers = {
"Authorization": f"Bearer {oauth_token}",
"Content-Type": "application/json"
}

# Send the POST request to create the webhook
response = requests.post(webhook_url, headers=headers, data=json.dumps(webhook_payload))

if response.status_code == 201:
print("Webhook created successfully.")
else:
print(f"Failed to create webhook. Status code: {response.status_code}")
print(response.text)

# Usage example
project_key = "KAN"
oauth_token = "your_oauth_token_here"
webhook_url = "https://your-webhook-url.com"

create_jira_webhook(project_key, oauth_token)





The error message I'm receiving is:


Failed to create webhook. Status code: 401
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<status>
<status-code>401</status-code>
<message>Client must be authenticated to access this resource.</message>
</status>
```

I've double-checked my OAuth token, and it seems to be correct. What could be causing this authentication issue, and how can I resolve it?

Any assistance or insights would be greatly appreciated. Thank you!

1 answer

0 votes
Marko Blagus
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 5, 2023

Hello @Satyajit,

 

Did you try with HTTP basic auth:

auth = HTTPBasicAuth("email@example.com", "<api_token>")

Add "auth" to your post request parameters.

And than remove your Authorization from headers, also add to "Accept": "application/json" to headers.

Let me know if this helps.

 

Best regards,

Marko Blagus

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
FREE
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events