简体   繁体   English

我可以使用 httplib2 进行抢先式身份验证吗?

[英]Can I do preemptive authentication with httplib2?

I need to perform preemptive basic authentication against an HTTP server, ie, authenticate right away without waiting on a 401 response.我需要对 HTTP 服务器执行抢先式基本身份验证,即立即进行身份验证,而无需等待 401 响应。 Can this be done with httplib2?这可以用httplib2完成吗?

Edit:编辑:

I solved it by adding an Authorization header to the request, as suggested in the accepted answer:我通过在请求中添加 Authorization header 来解决它,如接受的答案中所建议的那样:

 headers["Authorization"] = "Basic {0}".format(
        base64.b64encode("{0}:{1}".format(username, password)))

Add an appropriately formed 'Authorization' header to your initial request.在您的初始请求中添加适当格式的“授权”header。

This also works with the built-in httplib (for anyone wishing to minimize 3rd-party libs/modules).这也适用于内置的httplib (对于希望最小化 3rd-party 库/模块的任何人)。 I am using it to authenticate with our Jenkins server using the API Token that Jenkins can create for each user.我正在使用它使用 Jenkins 可以为每个用户创建的 API 令牌对我们的 Jenkins 服务器进行身份验证。

>>> import base64, httplib
>>> headers = {}
>>> headers["Authorization"] = "Basic {0}".format(
        base64.b64encode("{0}:{1}".format('<username>', '<jenkins_API_token>')))

>>> ## Enable the job
>>> conn = httplib.HTTPConnection('jenkins.myserver.net')
>>> conn.request('POST', '/job/Foo-trunk/enable', None, headers)
>>> resp = conn.getresponse()
>>> resp.status
302

>>> ## Disable the job
>>> conn = httplib.HTTPConnection('jenkins.myserver.net')
>>> conn.request('POST', '/job/Foo-trunk/disable', None, headers)
>>> resp = conn.getresponse()
>>> resp.status
302

I realize this is old, but I figured I'd throw in the solution if you're using Python 3 with httplib2 since I haven't been able to find it anywhere else.我意识到这已经过时了,但是我想如果您将 Python 3 与 httplib2 一起使用,我会提出解决方案,因为我无法在其他任何地方找到它。 I'm also authenticating against a Jenkins server using the API Token for each Jenkins user.我还在为每个 Jenkins 用户使用 API 令牌对 Jenkins 服务器进行身份验证。 If you're not concerned with Jenkins, simply substitute the actual user's password for the API Token.如果您不关心 Jenkins,只需将实际用户密码替换为 API 令牌即可。

b64encode is expecting an binary string of ASCII characters. b64encode 需要一个 ASCII 字符的二进制字符串。 With Python 3 a TypeError will be raised if a plain string is passed in. To get around this, the "user:api_token" portion of the header must be encoded using either 'ascii' or 'utf-8', passed to b64encode, then the resulting byte string must be decoded to a plain string before being placed in the header.对于 Python 3,如果传入纯字符串,则会引发 TypeError。为了解决此问题,header 的“user:api_token”部分必须使用“ascii”或“utf-8”进行编码,并传递给 b64encode,那么生成的字节字符串必须在放入 header 之前解码为纯字符串。 The following code did what I needed:下面的代码做了我需要的:

import httplib2, base64

cred = base64.b64encode("{0}:{1}".format(
    <user>, <api_token>).encode('utf-8')).decode()
headers = {'Authorization': "Basic %s" % cred}
h = httplib2.Http('.cache')
response, content = h.request("http://my.jenkins.server/job/my_job/enable",
    "GET", headers=headers)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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