2012-04-07 38 views
3

所以这应该很简单,但我似乎无法找到我的失败点。希望别人能指出我的意思。github API中的oauth流v3不工作

首先我去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 

当我去我的测试用户它显示我的授权和我的应用程序显示为具有一个用户,但我没有一个访问令牌。

我在做什么错。

回答

12

嗯,我是对的,这是一个非常简单的问题,但我会在这里让其他人遇到同样的错误。

如有疑问请手动定义您的标题。因此,你需要:

header = {'content-type':'application/json'} 

然后通过在标题:

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 
) 

对我来说这解决了这个问题。