【发布时间】:2019-09-28 06:45:20
【问题描述】:
我知道这似乎是一个非常重复的问题。从 bash 和 curl 我以这种方式调用 API Github
# curl -u myuser https://api.github.com/repos/myuser/somerepo/pulls?state=all -H "Authorization: token randomtokenhere" -d '{"title":"Pull Request develop to master","base":"master", "head":"develop"}'
并且像魅力一样工作。但是,从带有 json 和 requests 的 Python 3(3.5.2) 开始,无论如何我都会遇到错误。
例子:
user = "myuser"
password = "sompassword"
url = "https://api.github.com/repos/myuser/somerepo/pulls?state=all"
token = "randomtokenhere"
title = "Pull Request develop to master"
base = "master"
head = "develop"
headers = {'Authorization': 'token ' + token}
content = {"title":title,"base":base, "head":head}
req = requests.post(url,json=json.dumps(content),auth=(user,password),headers=headers)
print("response posts is {} and status code is {}".format(req.text,req.status_code))
请求的响应是
response posts is {"message":"Must specify two-factor authentication OTP code.","documentation_url":"https://developer.github.com/v3/auth#working-with-two-factor-authentication"} and status code is 401
所以看起来调用只是以某种方式丢失了令牌。但我不知道为什么。我可以以某种方式调试它吗?还是我错过了一些非常明显的东西?
谢谢
【问题讨论】:
标签: python github python-requests