Home > Software design >  add authentication info for urllib.request
add authentication info for urllib.request

Time:05-20

I had this query with with the requests library:

import requests

headers = {
    'Content-type': 'application/json',
}

data = """
{"target": {
  "ref_type": "branch",
  "type": "pipeline_ref_target",
  "ref_name": "main",
  "selector": {
    "type": "branches",
    "pattern" : "main"
    }
  }
}
"""

response = requests.post('https://api.bitbucket.org/2.0/repositories/name/{567899876}/pipelines/', headers=headers, data=data, auth=(username, password))
print(response.text)

Now, I want to do the same thing with the urllib.request or urllib3 preferably. I was trying this:

from urllib import request, parse

req =  request.Request('https://api.bitbucket.org/2.0/repositories/name/{4567898758}/pipelines/', method="POST", headers=headers, data=data, auth=(username, password))

resp = request.urlopen(req)
print(resp)

but urllib doesn't have an authparameter. I saw other examples online. For eg something like this:

auth_handler = url.HTTPBasicAuthHandler()
auth_handler.add_password(realm='Connect2Field API',
                          uri=urlp,
                          user='*****',
                          passwd='*****')

but I am not sure how to merge this with my existing headers in order to convert my request code to a urllib.request code.

CodePudding user response:

To be able to authenticate using urllib you'll need to use the base64 library to encode your credentials and adding them as header of your request.

from urllib import request, parse
import base64

req =  request.Request('https://api.bitbucket.org/2.0/repositories/name/{4567898758}/pipelines/', method="POST", headers=headers, data=data)

base64string = base64.b64encode(bytes('{}:{}'.format(username, password), 'ascii'))
req.add_header('Authorization', 'Basic {}'.format(base64string.decode('utf-8')))

resp = request.urlopen(req)

EDIT:

Using urllib3 you can do something like this

import urllib3

http = urllib3.PoolManager()
headers = urllib3.make_headers(basic_auth='{}:{}'.format(username, password))
resp = http.urlopen('POST', 'https://api.bitbucket.org/2.0/repositories/name/{4567898758}/pipelines/', headers=headers, data=data)
  • Related