我正在尝试为我的帐户访问 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。
我想知道:
我在请求版本中做错了什么,它们看起来都类似于我的 curl 尝试
使用 curl 和 python 请求模块请求有什么区别?阅读带有 curl 示例的 API 然后用 python 编写它时,我可以识别出什么通用模式?
谢谢
回答:
它需要正确的标题
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)