带有Python的Jira API在有效凭据上返回错误401

时间:2019-04-18 13:49:27

标签: python rest jira jira-rest-api

我正在尝试使用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)

这是所有代码。

最后一行失败

  

警告:root:从GET获得可恢复的错误   https://myaddress.atlassian.net/rest/api/2/serverInfo,将重试[1/3]   在13.906688704524315s中。错误:401

     

警告:root:有可恢复的错误   从GET https://myaddress.atlassian.net/rest/api/2/serverInfo中获取   在4.071181495745648s中重试[2/3]。错误:401

     

警告:root:Got   GET中的可恢复错误   https://myaddress.atlassian.net/rest/api/2/serverInfo,将重试[3/3]   在6.266303262421157s中。错误:401

在Atlassian上手动尝试使用凭据可以正常工作,并且我能够登录。

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

2 个答案:

答案 0 :(得分:2)

他们一直在讨论基本身份验证中弃用的密码。尝试生成一个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)

答案 1 :(得分:0)

请尝试以下代码:

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)