我正在尝试为我的帐户命中Bitbucket API,成功的尝试如下:
curl --user screename:mypassword https://api.bitbucket.org/1.0/user/repositories
在命令行。在python中,我尝试:
import requests
url = 'https://api.bitbucket.org/1.0/user/repositories'然后
r = requests.post(url, data={'username': myscreename, 'password':mypassword})和
r = requests.post(url, data="myscreename:mypassword")和
r = requests.post(url, data={"user": "myscreename:mypassword"})全部得到405错误。API是https://confluence.atlassian.com/bitbucket/rest-apis-222724129.html。
我想知道:
谢谢
答:
它需要正确的标题。
https://answers.atlassian.com/questions/18451025/answers/18451117?flashId=-982194107
更新:
# ===============
# get user
# ===============
import requests
import json
# [BITBUCKET-BASE-URL], i.e.: https://bitbucket.org/
url = '[BITBUCKET-BASE-URL]/api/1.0/user/'
headers = {'Content-Type': 'application/json'}
# get user
# [USERNAME], i.e.: myuser
# [PASSWORD], i.e.: itspassword
r = requests.get(url, auth=('[USERNAME]', '[PASSWORD]'), headers=headers)
print(r.status_code)
print(r.text)
#print(r.content)发布于 2015-06-25 21:46:06
下面是一种使用Python的请求模块完成基本HTTP的方法:
requests.post('https://api.bitbucket.org/1.0/user/repositories', auth=('user', 'pass'))另一种方式是传递用户/通过请求的有效负载,这是不需要的,因为在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
python的方式应该如下所示:
requests.post('https://api.bitbucket.org/1.0/repositories', auth=('user', 'pass'), data = "name=repo_name")发布于 2020-02-28 19:05:35
使用python3,您可以使用json={.}而不是data={.},它将自动将标头设置为application/json:
import requests
url = 'https://api.bitbucket.org/1.0/user/repositories'
data = {
'data1': 'asd',
'data2': 'asd'
}
req = requests.post(url, auth=('user', 'password'), json = data)
data = req.json()
# data['index']https://stackoverflow.com/questions/31061227
复制相似问题