<p>我是怎么解决这个问题的。感谢@Ivanzuzak请求B.in小费。在</p>
<p>我用的是pythonwebpy。在</p>
<pre><code>import web,requests
import oauth2,json
urls=('/', 'githublogin',
'/githubcallback','githubCallback');
render = web.template.render('templates/')
class githublogin:
def GET(self):
client_id = ''
url_string = "https://github.com/login/oauth/authorize?client_id=" + client_id
return render.index(url_string)
class githubCallback:
def GET(self):
data = json.loads(json.dumps(web.input()))
print data['code']
headers = {'content-type': 'application/json'}
pay_load = {'client_id': '',
'client_secret':'',
'code' : data['code'] }
r = requests.post('https://github.com/login/oauth/access_token', data=json.dumps(pay_load), headers=headers)
token_temp = r.text.split('&')
token = token_temp[0].split('=')
access_token = token[1]
repo_url = 'https://api.github.com/user?access_token=' + access_token
response = requests.get(repo_url)
final_data = response.content
print final_data
app = web.application(urls,globals())
if __name__ == "__main__":
app.run()
</code></pre>
<p>我以前没有使用html文件,而是直接从githublogin类发送请求。那没用。这里我使用一个html来引导用户首先从哪里登录到gh。在此基础上,我添加了一个html并使用templator呈现它。在</p>
^{pr2}$
<p>这个文件直接取自dev guide,只改变了client_id参数。在</p>
<p>另一点需要注意的是请求.post方法-直接传递pay_负载不起作用。必须使用json.dumps文件. 在</p>