以下是使用 Python 的 requests 模块进行基本 HTTP 身份验证的方法:
requests.post('https://api.bitbucket.org/1.0/user/repositories', auth=('user', 'pass'))
使用另一种方式,您可以通过请求的有效负载传递用户/密码,这是不需要的,因为 HTTP 基本身份验证在 HTTP 协议中拥有自己的位置。
如果您想“查看”您的请求的幕后情况,我建议使用 httpbin:
>>> url = "http://httpbin.org/post"
>>> r = requests.post(url, data="myscreename:mypassword")
>>> print r.text
{
"args": {},
"data": "myscreename:mypassword",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "22",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.5.1 CPython/2.7.6 Darwin/14.3.0"
},
"json": null,
"origin": "16.7.5.3",
"url": "http://httpbin.org/post"
}
>>> r = requests.post(url, auth=("myscreename", "mypassword"))
>>> print r.text
{
"args": {},
"data": "",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Authorization": "Basic bXlzY3JlZW5hbWU6bXlwYXNzd29yZA==",
"Content-Length": "0",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.5.1 CPython/2.7.6 Darwin/14.3.0"
},
"json": null,
"origin": "16.7.5.3",
"url": "http://httpbin.org/post"
}
并用卷曲:
curl -X POST --user myscreename:mypassword http://httpbin.org/post
{
"args": {},
"data": "",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Authorization": "Basic bXlzY3JlZW5hbWU6bXlwYXNzd29yZA==",
"Host": "httpbin.org",
"User-Agent": "curl/7.37.1"
},
"json": null,
"origin": "16.7.5.3",
"url": "http://httpbin.org/post"
}
请注意最后一个 Python 示例与 cURL 示例之间的相似之处。
现在,正确处理 API 的格式是另一回事了,请查看此链接:https://answers.atlassian.com/questions/94245/can-i-create-a-bitbucket-repository-using-rest-api https://answers.atlassian.com/questions/94245/can-i-create-a-bitbucket-repository-using-rest-api
python 的方式应该是这样的:
requests.post('https://api.bitbucket.org/1.0/repositories', auth=('user', 'pass'), data = "name=repo_name")