Basic authentication with jira-python

主宰稳场 提交于 2019-12-06 05:35:30

问题


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).

Using Python 2.7 and 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

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

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:

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:

import requests

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

Any suggestions?


回答1:


Here's how I use the jira module with authentication in a Python script:

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



回答2:


Below Python script connects to Jira and does basic authentication and lists all projects.

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.




回答3:


Don't change the library, instead put your credentials inside the ~/.netrc file.

If you put them there you will also be able to test your calls using curl or 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.

My lucky guess is that you broke it with that change.



来源:https://stackoverflow.com/questions/14078351/basic-authentication-with-jira-python

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