简体   繁体   中英

Github API returns 401 from python requests but 200 from curl

I'm aware seems a pretty repetitive issue. From bash and curl I make calls to the API Github this way

# 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"}'

and works like a charm. However, from Python 3(3.5.2) with json and requests, I got an error no matter what.

Example:

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))

the response of requests is

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

so it seems the call is just missing the token in some way. But I'm not able to know why. Can I debug this in some way? Or did I miss something very obvious?

Thanks

Well, sorry for bother, my call was not correct. This actually worked:

//req       = requests.post(url,json=json.dumps(content),auth=(user,password),headers=headers)
req     = requests.post(url,json=content,auth=(user,token))

I've removed the json.dumps function, removed the headers parameter, and instead use password I set up the token with the auth.

Regards

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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