2012-04-07 35 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 
) 

對我來說這解決了這個問題。