如何让django接入新浪微博OAuth的方法,djangooauth,本篇python教程学习
如何让django接入新浪微博OAuth的方法,djangooauth,本篇python教程学习
本篇python教程学习中的源码,是关于如何让django接入新浪微博OAuth的方法。因为有网友提出要将网站内容和微博整合起来,意思就是将页面内容和新浪微博联系起来。这样形成一个独立的内容页面,对应一条微博。你会想到如果用户需要发表评论的话,肯定要接入oauth。因为你不能强制用户登录你的网站来发评论。所以在了解了新浪微博的授权机制之后,就下载了python版的sdk。之后就可以在django上接入oauth了。
对oauth不了解的朋友,可以自行百度一下。
简单的来说一下流程,其实流程很简单:get_request_token -> create_auth_url -> [user_login: 跳转到新浪登录页面,用户登陆后会跳转回来] -> get_access_token -> done!如何让django接入新浪微博OAuth的方法,python源码如下:
#!/usr/bin/env python# -*- coding: utf-8 -*-"""基于django的新浪微博oauth views需要django的session支持"""from django.http import HttpResponseRedirectfrom weibopy import OAuthHandler, oauth, WeibopErrorconsumer_key = '' # 设置你申请的appkeyconsumer_secret = '' # 设置你申请的appkey对于的secretclass WebOAuthHandler(OAuthHandler): def get_authorization_url_with_callback(self, callback, signin_with_twitter=False): """Get the authorization URL to redirect the user""" try: # get the request token self.request_token = self._get_request_token() # build auth request and return as url if signin_with_twitter: url = self._get_oauth_url('authenticate') else: url = self._get_oauth_url('authorize') request = oauth.OAuthRequest.from_token_and_callback( token=self.request_token, callback=callback, http_url=url ) return request.to_url() except Exception, e: raise WeibopError(e)def _get_referer_url(request): referer_url = request.META.get('HTTP_REFERER', '/') host = request.META['HTTP_HOST'] if referer_url.startswith('http') and host not in referer_url: referer_url = '/' # 避免外站直接跳到登录页而发生跳转错误 return referer_urldef _oauth(): """获取oauth认证类""" return WebOAuthHandler(consumer_key, consumer_secret)def login(request): # 保存最初的登录url,以2000便认证成功后跳转回来 back_to_url = _get_referer_url(request) request.session['login_back_to_url'] = back_to_url # 获取oauth认证url login_backurl = request.build_absolute_uri('/login_check') auth_client = _oauth() auth_url = auth_client.get_authorization_url_with_callback(login_backurl) # 保存request_token,用户登录后需要使用它来获取access_token request.session['oauth_request_token'] = auth_client.request_token # 跳转到登录页面 return HttpResponseRedirect(auth_url) def login_check(request): """用户成功登录授权后,会回调此方法,获取access_token,完成授权""" # http://mk2.com/?oauth_token=c30fa6d693ae9c23dd0982dae6a1c5f9&oauth_verifier=603896 verifier = request.GET.get('oauth_verifier', None) auth_client = _oauth() # 设置之前保存在session的request_token #www.iplaypy.com request_token = request.session['oauth_request_token'] del request.session['oauth_request_token'] auth_client.set_request_token(request_token.key, request_token.secret) access_token = auth_client.get_access_token(verifier) # 保存access_token,以后访问只需使用access_token即可 request.session['oauth_access_token'] = access_token # 跳转回最初登录前的页面 back_to_url = request.session.get('login_back_to_url', '/') return HttpResponseRedirect(back_to_url)def logout(request): """用户登出,直接删除access_token""" del request.session['oauth_access_token'] back_to_url = _get_referer_url(request) return HttpResponseRedirect(back_to_url)
关于在django上结合python版的sdk的具体实现代码,已经有很详细的注释了,希望各位能看明白其中的原理,再加以改进使用。
编橙之家文章,
相关内容
- web.py页面执行计时,Python装饰器实现方法,web.pypython
- Python替换字符串空格的处理方法,python字符串,python如何
- 用python计算1到100的和源码详解,python100源码详解,用p
- 调用Bing桌面更换为本地桌面的python方法,bingpython,这是
- Python编写的俄罗斯方块游戏案例源码详解,PYQT4,pyth
- python采集文章中图片的方法源码,,这是收集的一篇关于
- Python tar、zip压缩文件操作方法,pythontar,python语言源码
- Python调用摄像头摄像并保存图片的方法源码,,用pytho
- Python 类快速排序方法找到第定n小数的方法,python小数
- Python实现SYSLOG服务器与MYSQL数据库写入源码,pythonsysl
评论关闭