使用 jira-python 进行基本身份验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14078351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Basic authentication with jira-python
提问by Neal Caidin
I'm new to Python, new to the jira-python library, and new to network programming, though I do have quite a bit of experience with application and integration programming and database queries (though it's been a while).
我是 Python 新手、jira-python 库新手和网络编程新手,尽管我在应用程序和集成编程以及数据库查询方面确实有相当多的经验(虽然已经有一段时间了)。
Using Python 2.7 and requests 1.0.3
使用 Python 2.7 和 requests 1.0.3
I'm trying to use this library - http://jira-python.readthedocs.org/en/latest/to query Jira 5.1 using Python. I successfully connected using an unauthenticated query, though I had to make a change to a line in client.py, changing
我正在尝试使用这个库 - http://jira-python.readthedocs.org/en/latest/使用 Python 查询 Jira 5.1。我使用未经身份验证的查询成功连接,但我必须对中的一行进行client.py更改,更改
I changed
我变了
self._session = requests.session(verify=verify, hooks={'args': self._add_content_type})
to
到
self._session = requests.session()
I didn't know what I was doing exactly but before the change I got an error and after the change I got a successful list of project names returned.
我不知道我在做什么,但在更改之前我收到了一个错误,更改之后我得到了一个成功的项目名称列表。
Then I tried basic authentication so I can take advantage of my Jira permissions and do reporting. That failed initially too. And I made the same change to
然后我尝试了基本身份验证,这样我就可以利用我的 Jira 权限并进行报告。最初也失败了。我做了同样的改变
def _create_http_basic_session
in client.py, but now I just get another error. So problem not solved. Now I get a different error:
in 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).
So then I decided to do a super simple test just using the requests module, which I believe is being used by the jira-python module and this code seemed to log me in. I got a good response:
所以我决定只使用 requests 模块做一个超级简单的测试,我相信 jira-python 模块正在使用它,这段代码似乎让我登录。我得到了很好的回应:
import requests
r = requests.get(the_url, auth=(my username , password))
print r.text
Any suggestions?
有什么建议?
回答by mdoar
Here's how I use the jiramodule with authentication in a Python script:
下面是我如何在 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
回答by sorin
Don't change the library, instead put your credentials inside the ~/.netrc file.
不要更改库,而是将您的凭据放在 ~/.netrc 文件中。
If you put them there you will also be able to test your calls using curl or wget.
如果你把它们放在那里,你还可以使用 curl 或 wget 测试你的调用。
I am not sure anymore about compatibility with Jira 5.x, only 7.x and 6.4 are currently tested. If you setup an instance for testing I could modify the integration tests to run against it, too.
我不确定与 Jira 5.x 的兼容性,目前只测试了 7.x 和 6.4。如果你设置了一个测试实例,我也可以修改集成测试来运行它。
My lucky guess is that you broke it with that change.
我的幸运猜测是,你用那个改变打破了它。
回答by Balvant Biradar
Below Python script connects to Jira and does basic authentication and lists all projects.
下面的 Python 脚本连接到 Jira 并进行基本身份验证并列出所有项目。
from jira.client import JIRA
options = {'server': 'Jira-URL'}
jira = JIRA(options, basic_auth=('username', 'password'))
projects = jira.projects()
for v in projects:
print v
It prints a list of all the project's available within your instance of Jira.
它会打印 Jira 实例中所有可用项目的列表。

