Python API update

Joe Simonsen December 3, 2019

Been searching around for a solid day and can't seem to find what I'm looking for. Trying to run a python script to update a single custom field on JIRA Service Desk. I originally wrote a script that worked :

```

from jira import JIRA

def main():

options = {'server': 'https://xxx.atlassian.net'}
jira = JIRA(options, basic_auth=("jUSER", "API_Key"))
issue = jira.issue('GISSUE-123')

issue.update(fields={'customfield_10130': 'New Value'})

print (issue.fields.project.key)
print (issue.fields.issuetype.name)
print (issue.fields.reporter.displayName)
print (issue.fields.summary)
print (issue.fields.project.id)
print(issue.fields.customfield_10130)


if __name__== "__main__" :
main()

```

However I am unable to user the JIRA module in this environment for reasons. I can however use requests which led me to write:

```

import requests
from requests.auth import HTTPBasicAuth
import json

url = "https://xxx.atlassian.net/rest/api/3/issue/ISSUE-123"

auth = HTTPBasicAuth("USER", "KEY")

update = {'customfield_10130': 'New Value'}

headers = {"Accept": "application/json"}

x = requests.post(url, json=update, headers=headers, auth=auth)

```

BUt this doesn't seem to do anything. I'm probably configuring this wrong as I don't have much experience with the request module here. 

Any help or advice would be great!

1 answer

0 votes
Andy Heinzer
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
December 10, 2019

Hi Joe,

I see that you are trying to make some REST calls to Jira Cloud using python here.  I understand that the old script used to work, but I suspect that the Deprecation notice - Basic authentication with passwords and cookie-based authentication  likely is affecting this request attempt.  Eventhough it looks like you're using a REST API token instead of a password, in my experience we can't just pass that token in cleartext anymore.

Instead I believe you will need to consult with the Basic auth for REST APIs.  Essentially, I think you can make this work, but instead of trying to get the authorization with a username/token (which used to work), instead you need to create a string of

user_email_address:API_token_string

and then base64 encode that string.  You can then pass that string as part of an authorization header to make able to make an authenticated REST API request.

Try that approach instead.  Let me know if you run into any problems here.

Cheers,

Andy

Suggest an answer

Log in or Sign up to answer