繁体   English   中英

Github API从python请求中返回401,但从curl返回200

[英]Github API returns 401 from python requests but 200 from curl

我知道这似乎是一个非常重复的问题。 从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

所以看来这个电话只是以某种方式错过了令牌。 但我不知道为什么。 我可以用某种方式调试吗? 还是我错过了一些非常明显的东西?

谢谢

好吧,抱歉打扰,我的电话不正确。 这实际上有效:

//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设置了令牌。

问候

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM