Bitbucket API Authentication Using Python HTTPBasicAuthHandler

I am trying to get a list of problems in a private repository using the bitbucket API .

I have confirmed that HTTP Basic authentication works with hurl , but I cannot authenticate with Python. Adapting the code from this tutorial , I wrote the following script.

import cookielib
import urllib2

class API():
    api_url = 'http://api.bitbucket.org/1.0/'

    def __init__(self, username, password):
        self._opener = self._create_opener(username, password)

    def _create_opener(self, username, password):
        cj = cookielib.LWPCookieJar()
        cookie_handler = urllib2.HTTPCookieProcessor(cj)
        password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
        password_manager.add_password(None, self.api_url, username, password)
        auth_handler = urllib2.HTTPBasicAuthHandler(password_manager)
        opener = urllib2.build_opener(cookie_handler, auth_handler)
        return opener

    def get_issues(self, username, repository):
        query_url = self.api_url + 'repositories/%s/%s/issues/' % (username, repository)
        try:
            handler = self._opener.open(query_url)
        except urllib2.HTTPError, e:
            print e.headers
            raise e
        return handler.read()

api = API(username='my_username', password='XXXXXXXX')

api.get_issues('my_username', 'my_repository') leads to:

>>> 
Server: nginx/0.7.62
Date: Mon, 19 Apr 2010 16:15:06 GMT
Content-Type: text/plain
Connection: close
Vary: Authorization,Cookie
Content-Length: 9

Traceback (most recent call last):
  File "C:/USERS/personal/bitbucket-burndown/bitbucket-api.py", line 29, in <module>
    print api.get_issues('my_username', 'my_repository')
  File "C:/USERS/personal/bitbucket-burndown/bitbucket-api.py", line 25, in get_issues
    raise e
HTTPError: HTTP Error 401: UNAUTHORIZED

api.get_issues('jespern', 'bitbucket') works like a charm.

What is wrong with my code?

+3
source share
4 answers

There seems to be a problem with HTTPBasicAuthHandler . It works:

class API():
    api_url = 'http://api.bitbucket.org/1.0/'

    def __init__(self, username, password, proxy=None):
        encodedstring = base64.encodestring("%s:%s" % (username, password))[:-1]
        self._auth = "Basic %s" % encodedstring
        self._opener = self._create_opener(proxy)

    def _create_opener(self, proxy=None):
        cj = cookielib.LWPCookieJar()
        cookie_handler = urllib2.HTTPCookieProcessor(cj)
        if proxy:
            proxy_handler = urllib2.ProxyHandler(proxy)
            opener = urllib2.build_opener(cookie_handler, proxy_handler)
        else:
            opener = urllib2.build_opener(cookie_handler)
        return opener

    def get_issues(self, username, repository):
        query_url = self.api_url + 'repositories/%s/%s/issues/' % (username, repository)
        try:
            req = urllib2.Request(query_url, None, {"Authorization": self._auth })
            handler = self._opener.open(req)
        except urllib2.HTTPError, e:
            print e.headers
            raise e
        return json.load(handler)
+4
source

, Python HTTPBasicAuthHandler. :

  • ;
  • 401;
  • ;
  • .

BitBucket :

  • ;
  • BitBucket .

BitBucket 401, Python .

BitBucket cookie:

+2

python-bitbucket, python API Bitbucket. , ericof , : https://bitbucket.org/bkmontgomery/python-bitbucket

:

import bitbucket

bb = bitbucket.BitBucket(username='your-username', password='secret')
repo = bb.repository('your-username', 'your-private-repo')
issues = repo.issues()
+1
source

You can try to subclass HTTPPasswordMgr and override the find_user_password () method to see where your code refuses to find the password. I assume add_password () is not doing what you expect.

0
source

Source: https://habr.com/ru/post/1741809/


All Articles