liboauth2是一个轻量级的oauth2python客户端。
liboauth2只是实现oauth2协议,并没有实现任何第三方oauth2认证。
如果需要实现第三登录,可以看pysns
支持四种认证方式
一、AuthorizationCode授权码方式
二、ImplicitGrant隐式授权
三、ResourceOwnerPasswordCredentials
四、ClientCredentials客户端证书授权
安装
easy_installliboauth2腾讯微博演示
#-*-coding:utf-8-*-importliboauth2importurllibfromflaskimportFlask,redirect,requestCLIENT_ID='youclientid'CLIENT_SECRET='youclientsecret'client=liboauth2.Client(CLIENT_ID,CLIENT_SECRET)REDIRECT_URI='https://localhost:5000/callback/'AUTH_URI='https://open.t.qq.com/cgi-bin/oauth2/authorize'ACCESS_TOKEN_URL='https://open.t.qq.com/cgi-bin/oauth2/access_token'app=Flask(__name__)app.debug=True@app.route('/')defhome():#getauthurlurl=client.get_auth_url(AUTH_URI,REDIRECT_URI)returnredirect(url)@app.route('/callback/')defcallback():#getaccesstokenparams={'code':request.args['code'],'redirect_uri':REDIRECT_URI}resp=client.get_access_token(ACCESS_TOKEN_URL,liboauth2.GRANT_TYPE_AUTH_CODE,params)#printrespdefurldecode(values):ret={}forsinvalues.split('&'):ifs.find('=')>-1:k,v=map(urllib.unquote,s.split('='))ret[k]=v#ret.setdefault(k,[]).append(v)returnretdata=urldecode(resp['result'])#setaccesstokenandqueryuserinfoclient.set_access_token(data['access_token'])params={'oauth_consumer_key':CLIENT_ID,'openid':data['openid'],'clientip':request.remote_addr,'oauth_version':'2.a',}resp=client.fetch('https://open.t.qq.com/api/user/info',params)returnstr(resp['result'])if__name__=='__main__':app.run(host='localhost',port=5000)
评论