How Django accesses Sina Weibo OAuth

Source: Internet
Author: User
Tags comments oauth

This example describes the way Django accesses Sina Weibo oauth. Share to everyone for your reference. The specific analysis is as follows:

The recent integration of the Web site and Sina Weibo, the idea is very simple, is to link the content of the page and Sina Weibo, an independent content of the page for a micro-blog, the nature of the comment system only need to use Weibo comments. Then, users need to comment, must be connected to OAuth, it is impossible for users to log on to your site to send comments? No one will tell you their account number and password. Look at the licensing mechanism for Sina Weibo and then download the Python version of the SDK to connect OAuth on Django.

For OAuth very unfamiliar classmate, please first look at the OAuth protocol introduction

In fact, the process is simple:

①getrequesttoken->

②createauthurl->

③[user_login: Jump to Sina login page, users will jump back after landing]->

④getaccesstoken->

⑤done!

The detailed implementation code of the Python version of the SDK on Django is already commented on in detail:

oauth_views.py files are as follows:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67-68 #!/usr/bin/env python #-*-coding:utf-8-*-"" "based on the Django Sina Weibo oauth views require Django session support" "" from django.http Import Httpresponseredirect from weibopy import Oauthhandler, OAuth, Weiboperror consumer_key = ' # Set up your application for Appkey Consumer_secre t = ' # Set the Appkey for your application for secret class 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 SE Lf.request_token = Self._get_request_token () # Build Auth request and return as URL if Signin_with_twitter:url = Self._ge T_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 = '/' # Avoid outbound jumps directly to the login page and a jump error return Referer_url def _oauth (): "" "" "Get OAuth Authentication Class" "" Return Weboauthhandler (Consumer_key, Consumer_secret) def login (Request): # Save the original login URL so that the authentication succeeds and then jumps back Back_to_url = _get_ Referer_url (Request) request.session[' login_back_to_url '] = back_to_url # get oauth authentication url Login_backurl = request.build_ Absolute_uri ('/login_check ') auth_client = _oauth () Auth_url = Auth_client.get_authorization_url_with_callback (Login _backurl) # Save Request_token, user login needs to use it to obtain Access_token request.session[' oauth_request_token '] = Auth_client.request_ Token # Jump to login page return Httpresponseredirect (auth_url) def login_check (Request): "" "When a user successfully logs on to the authorization, it will callback this method, get Access_token, Complete the Authorization "" # http://mk2.com/?oauth_token=c30fa6d693ae9c23dd0982dae6a1c5f9&oauth_verifier=603896 verifier = Request . Get.get (' Oauth_verifier ', None) auth_client = _oauth () # Set before saving in session Request_token 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) # Save Access_token, only use Access_token later to request.session[' oauth_access_token '] = access_token # Jump back to the page before the initial login Back_to_url = request.session.get (' Login_back_to_url ', '/') return Httpresponseredirect (Back_to_url) def Logout (Request): "" "The user log out, delete the Access_token directly" "" "Del request.session[' Oauth_access_token '] Back_to_url = _get_referer_ URL (Request) return Httpresponseredirect (Back_to_url)

I hope this article will help you with your Python programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.