简体   繁体   中英

Python-Jenkins API Connection Error

I'm trying to use python-jenkins to connect to a jenkins instance.

My code snippet is as follows:

import jenkins

JENKINS_URL = 'xxxx'
JENKINS_USER = 'yyyy'
JENKINS_PASSWORD = 'zzzz'

server = jenkins.Jenkins(JENKINS_URL, username=JENKINS_USER, password=JENKINS_PASSWORD)

jobs = server.get_jobs()

for job in jobs:
    print "Job: {0}".format(job.name)

The username and password are valid; I can log in to jenkins via the web using them without issue. However, here, I get an error. The traceback I get is as follows:

Traceback (most recent call last):
    File "apitest.py", line 11, in <module>
        jobs = server.get_jobs()
    File "/usr/lib/python2.7/site-packages/jenkins/__init__.py", line 540, in get_jobs
        return self.get_info()['jobs']
    File "/usr/lib/python2.7/site-packages/jenkins/__init__.py", line 422, in get_info
        Request(self.server + INFO)))
    File "/usr/lib/python2.7/site-packages/jenkins/__init__.py", line 312, in jenkins_open
        self.maybe_add_crumb(req)
    File "/usr/lib/python2.7/site-packages/jenkins/__init__.py", line 230, in maybe_add_crumb
        self.server + CRUMB_URL), add_crumb=False)
    File "/usr/lib/python2.7/site-packages/jenkins/__init__.py", line 330, in jenkins_open
        e.code, e.msg)
jenkins.JenkinsException: Error in request. Possibly authentication failed [500]: Server Error

Can anyone shed any light on the problem? I'm using python-jenkins version 0.4.8, and our jenkins box is version 1.541. The only other useful data point I can think of is that the jenkins box is configured to support https access only.

NB I've also tried jenkinsapi, but get a similar error

The problem here was that I was using the password for the account, whereas actually you need to use the API token.

The documentation (and variable names) in jenkinsapi don't make this clear at all!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM