使用Python的Bitbucket API身份验证';s HTTPBasicAuthHandler
我正在尝试使用获取私有存储库中的问题列表 我已经确认HTTP基本身份验证可以使用,但我无法使用Python进行身份验证。根据这段代码,我编写了以下脚本使用Python的Bitbucket API身份验证';s HTTPBasicAuthHandler,python,urllib2,bitbucket,bitbucket-api,Python,Urllib2,Bitbucket,Bitbucket Api,我正在尝试使用获取私有存储库中的问题列表 我已经确认HTTP基本身份验证可以使用,但我无法使用Python进行身份验证。根据这段代码,我编写了以下脚本 import cookielib import urllib2 class API(): api_url = 'http://api.bitbucket.org/1.0/' def __init__(self, username, password): self._opener = self._create_o
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\u问题(“我的用户名”、“我的存储库”)
导致:
>>>
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
>
服务器:nginx/0.7.62
日期:2010年4月19日星期一格林尼治标准时间16:15:06
内容类型:文本/纯文本
连接:关闭
更改:授权、Cookie
内容长度:9
回溯(最近一次呼叫最后一次):
文件“C:/USERS/personal/bitbucket burndown/bitbucket api.py”,第29行,在
打印api.get\u问题(“我的\u用户名”、“我的\u存储库”)
文件“C:/USERS/personal/bitbucket burndown/bitbucket api.py”,第25行,在get_问题中
提高e
HTTPError:HTTP错误401:未经授权
api.get\u问题('jespern','bitbucket')
工作起来就像一个符咒
我的代码有什么问题?您可以尝试对HTTPPasswordMgr进行子分类,并覆盖find_user_password()方法,以查看您的代码放弃查找密码的地方。我的猜测是add_password()并没有完全按照您的预期进行操作。它似乎存在问题。这项工作:
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)
我认为Python的HTTPBasicAuthHandler中没有错误。基本身份验证通常遵循以下过程:
- 客户端在没有凭据的情况下发出请求李>
- 服务器响应401错误李>
- 客户端使用凭据重新发送请求李>
- 服务器用内容进行响应
- 客户端在没有凭据的情况下发出请求李>
- BitBucket响应公共存储库列表
import bitbucket
bb = bitbucket.BitBucket(username='your-username', password='secret')
repo = bb.repository('your-username', 'your-private-repo')
issues = repo.issues()
我尝试过这个方法,但似乎没有调用find_user_password(),但我不明白为什么。无论如何,谢谢你:)