使用 jira-python 进行基本身份验证

新手上路,请多包涵

我是 Python 的新手,是 jira-python 库的新手,也是网络编程的新手,尽管我在应用程序和集成编程以及数据库查询方面确实有相当多的经验(尽管已经有一段时间了)。

使用 Python 2.7 和请求 1.0.3

我正在尝试使用此库 - http://jira-python.readthedocs.org/en/latest/ 使用 Python 查询 Jira 5.1。我使用未经身份验证的查询成功连接,尽管我必须更改 client.py 中的一行,更改

我变了

self._session = requests.session(verify=verify, hooks={'args': self._add_content_type})

self._session = requests.session()

我不知道我到底在做什么,但在更改之前我得到了一个错误,在更改之后我得到了一个成功的项目名称列表。

然后我尝试了基本身份验证,这样我就可以利用我的 Jira 权限并进行报告。最初也失败了。我做了同样的改变

def _create_http_basic_session

client.py 中,但现在我又遇到了另一个错误。所以问题没有解决。现在我得到一个不同的错误:

 HTTP Status 415 - Unsupported Media Type
type Status report
message Unsupported Media Type

description The server refused this request because the request entity is in
a format not` `supported by the requested resource for the requested method
(Unsupported Media Type).

所以我决定只使用 requests 模块做一个超级简单的测试,我相信 jira-python 模块正在使用它,这段代码似乎让我登录。我得到了很好的回应:

 import requests

r = requests.get(the_url, auth=(my username , password))
print r.text

有什么建议么?

原文由 Neal Caidin 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 455
1 个回答

以下是我如何在 Python 脚本中使用带有身份验证的 jira 模块:

 from jira.client import JIRA
import logging

# Defines a function for connecting to Jira
def connect_jira(log, jira_server, jira_user, jira_password):
    '''
    Connect to JIRA. Return 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 None

# create logger
log = logging.getLogger(__name__)

# NOTE: You put your login details in the function call connect_jira(..) below!

# create a connection object, jc
jc = connect_jira(log, "https://myjira.mydom.com", "myusername", "mypassword")

# print names of all projects
projects = jc.projects()
for v in projects:
       print v

原文由 mdoar 发布,翻译遵循 CC BY-SA 3.0 许可协议

推荐问题