【问题标题】:Github API returns 401 from python requests but 200 from curlGithub API 从 python 请求返回 401,但从 curl 返回 200
【发布时间】: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


    【解决方案1】:

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

    //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 参数,而是使用密码我设置了带有身份验证的令牌。

    问候

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-23
      • 2019-06-25
      • 1970-01-01
      • 2017-12-05
      • 2017-07-16
      • 1970-01-01
      • 1970-01-01
      • 2017-10-30
      相关资源
      最近更新 更多