Jira API with python returns error 401 on valid credentials

杀马特。学长 韩版系。学妹 提交于 2019-12-11 08:23:52

问题


I am trying to use the Jira python library to do some quite basic things. Even before doing anything, the constructor fails.

address = 'https://myaddress.atlassian.net'
options = {'server': address}
un = 'my@user.com'
#un = 'my' #also doesn't work
pw = 'the_pasSword!'
cookie = (un, pw)

j = JIRA(options, basic_auth=cookie)

This is ALL the code.

The last line fails with

WARNING:root:Got recoverable error from GET https://myaddress.atlassian.net/rest/api/2/serverInfo, will retry [1/3] in 13.906688704524315s. Err: 401

WARNING:root:Got recoverable error from GET https://myaddress.atlassian.net/rest/api/2/serverInfo, will retry [2/3] in 4.071181495745648s. Err: 401

WARNING:root:Got recoverable error from GET https://myaddress.atlassian.net/rest/api/2/serverInfo, will retry [3/3] in 6.266303262421157s. Err: 401

Trying the credentials manually on atlassian do work, and I am able to log in.

Any idea why this very straightforward attempt to connect wouldn't work?


回答1:


They have been discussing deprecating passwords in basic auth. Try generating an API token and using that in replacement of your password.

https://developer.atlassian.com/cloud/jira/platform/deprecation-notice-basic-auth-and-cookie-based-auth/

address = 'https://myaddress.atlassian.net'
options = {'server': address}
un = 'my@user.com'
#un = 'my' #also doesn't work
token = 'the_tokEn'
cookie = (un, token)

j = JIRA(options, basic_auth=cookie)



回答2:


Please try this code:

from jira.client import JIRA
import logging
import getpass
import datetime
import os

# Clearing the screen
os.system('cls||clear')
# Getting user authentication data
print 'Please enter your authentication data'
USER = raw_input('Username: ')
PASSWORD = getpass.getpass('Password: ')
print
JIRA_URL = "YOUR_JIRA_URL"
i = datetime.datetime.now()
TODAY = ("%s/%s/%s" % (i.day, i.month, i.year) )

def connect_jira(log, jira_server, jira_user, jira_password):
    '''
    Connects to JIRA

    Returns None on error
    '''
    try:
        log.info("Connecting to JIRA: %s" % jira_server)
        jira_options = {'server': jira_server}
        jira = JIRA(options=jira_options, basic_auth=(jira_user, jira_password))
                                        # ^--- Note the tuple
        return jira
    except Exception,e:
        log.error("Failed to connect to JIRA: %s" % e)
        return e

# Creating logger
log = logging.getLogger(__name__)
# Creating a Jira connection object, jc
jc = connect_jira(log, JIRA_URL, USER, PASSWORD)


来源:https://stackoverflow.com/questions/55747461/jira-api-with-python-returns-error-401-on-valid-credentials

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!