我知道这似乎是一个非常重复的问题。从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和请求的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
因此,通话似乎只是以某种方式丢失了令牌。但是我不知道为什么。我可以用某种方式调试吗?还是我错过了很明显的事情?
谢谢
答案 0 :(得分:0)
好,抱歉,我的电话不正确。这实际上有效:
//req = requests.post(url,json=json.dumps(content),auth=(user,password),headers=headers)
req = requests.post(url,json=content,auth=(user,token))
我删除了json.dumps函数,删除了headers参数,而是使用密码通过auth设置了令牌。
致谢