使用令牌在Python中运行Jenkins作业

使用令牌在Python中运行Jenkins作业,python,jenkins,request,Python,Jenkins,Request,我想用代币来运行Jenkins作业。 但这段代码给出了403错误 如何避免这个问题?我不会使用用户名和密码,只使用令牌。 有没有办法做到这一点 代码: import requests try: build = requests.get("http://jenkins_url/jenkins_job_name/build?token=TokenFromJob") except Exception as e: print ("Failed trigger

我想用代币来运行Jenkins作业。 但这段代码给出了403错误

如何避免这个问题?我不会使用用户名和密码,只使用令牌。 有没有办法做到这一点

代码:

import requests
try:
    build = requests.get("http://jenkins_url/jenkins_job_name/build?token=TokenFromJob")
except Exception as e:
    print ("Failed triggering the Jenkins job")
print (build.status_code)
参考。詹金斯不做授权。因此,即使生成了授权密钥,也需要在python脚本中处理授权

请注意,Jenkins不进行任何授权协商。i、 e.它 立即返回403(禁止)响应,而不是401响应 (未经授权)响应,因此请确保发送身份验证 来自第一个请求的信息(也称为“抢占式身份验证”)

以下是我的设置。需要传递用户名和api令牌,如下所示。登录后,不需要将令牌传递给构建url

import requests
from requests.auth import HTTPBasicAuth

session = requests.Session()
login_response = session.post(JENKINS_URL,auth=HTTPBasicAuth(USERNAME,API_TOKEN))
# check is status code is successfull
# then do other things
build = requests.get("http://jenkins_url/jenkins_job_name/build")
print (build.status_code)

非常感谢:)