Jira API 与 python returns 有效凭据上的错误 401

Jira API with python returns error 401 on valid credentials

我正在尝试使用 Jira python 库来做一些非常基本的事情。 甚至在做任何事情之前,构造函数就失败了。

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)

这是全部代码。

最后一行失败

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

在 atlassian 上手动尝试凭据确实有效,我能够登录。

知道为什么这种非常直接的连接尝试不起作用吗?

他们一直在讨论在基本身份验证中弃用密码。尝试生成一个 API 令牌并使用它来替换您的密码。

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)

请试试这个代码:

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)