Skip to content Skip to sidebar Skip to footer

Bitbucket Api Authentication With Python's Httpbasicauthhandler

I'm trying to get the list of issues on a private repository using bitbucket's API. I have confirmed that HTTP Basic authentication works with hurl, but I am unable to authenticate

Solution 1:

It looks like there is an issue with HTTPBasicAuthHandler. This works:

classAPI():
    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

    defget_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)

Solution 2:

I don't think there's an error in Python's HTTPBasicAuthHandler. Basic authentication usually follows this process:

  • Client makes request without credentials;
  • Server responds with 401 error;
  • Client resends request with credentials;
  • Server responds with content.

In the case of BitBucket, this happens:

  • Client makes request without credentials;
  • BitBucket responds with list of public repositories.

BitBucket never issues the 401, so Python never sends the credentials.

Have a look at this example for authenticating with BitBucket without using cookies:

Solution 3:

Take a look at python-bitbucket, which is a python wrapper for Bitbucket's API. The original library only includes read access, but ericof included authentication and some write access, which I've pulled in and built upon here: https://bitbucket.org/bkmontgomery/python-bitbucket

You could retrieve Issues from a private repo as follows:

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

Solution 4:

You could try sub-classing HTTPPasswordMgr and overriding the find_user_password() method to see where your code is giving up on finding the password. My guess is that add_password() isn't doing exactly what you'd expect.

Post a Comment for "Bitbucket Api Authentication With Python's Httpbasicauthhandler"