github api v3中的oauth流无法正常工作

Dan*_*ill 4 python api oauth github

所以这应该很简单,但我似乎无法找到我的失败点.希望其他人可以向我指出.

首先我去https://github.com/login/oauth/authorize?client_id=CLIENT_ID&scope=gist,这将返回一个代码给我.然后我这样做:

import requests, json

client_id = XXXX
client_secret = XXXX
code = XXXX

r = requests.post(
    'https://github.com/login/oauth/access_token', 
    data=json.dumps({
        'client_id':client_id, 
        'client_secret':client_secret,
        'code':code
    })
r.content  # this gives me a 404 header
Run Code Online (Sandbox Code Playgroud)

当我转到我的测试用户时,它会将我显示为已授权,并且我的应用显示为拥有一个用户,但我没有访问令牌.

我究竟做错了什么.

Dan*_*ill 12

嗯,我说得对,这是一个非常简单的问题,但是我会把它留在这里,因为其他人遇到了同样的错误.

如有疑问,请手动定义标题.所以你需要:

header = {'content-type':'application/json'}
Run Code Online (Sandbox Code Playgroud)

然后传入标题:

r = requests.post(
    'https://github.com/login/oauth/access_token', 
    data=json.dumps({
        'client_id':client_id, 
        'client_secret':client_secret,
        'code':code
    }),
    headers=header
)
Run Code Online (Sandbox Code Playgroud)

对我来说,这解决了这个问题.