How Django accesses Sina Weibo's OAuth

Source: Internet
Author: User
Tags oauth
This article is an example of how Django accesses Sina Weibo oauth. Share to everyone for your reference. The specific analysis is as follows:

The recent integration of the site and Sina Weibo, the idea is simple, is to link the content of the page and Sina Weibo, a separate content page for a micro-blog, the natural comment system only need to use Weibo comments. Then, users need to post a comment, it is necessary to access OAuth, it is not possible for users to log on to your website to comment it? No one will tell you their account number and password. After reviewing the licensing mechanism for Sina Weibo, and then downloading the Python version of the SDK, you can access OAuth on Django.

For students who are unfamiliar with OAuth, please review the OAuth protocol introduction first

In fact, the process is simple:

①getrequesttoken
②createauthurl
③[user_login: Jump to the Sina login page, the user will jump back after landing],
④getaccesstoken
⑤done!

The specific implementation code of the Python version of the SDK on Django has been commented in detail:

The oauth_views.py file is as follows:

#!/usr/bin/env python#-*-coding:utf-8-*-"" "Django-based Sina Weibo OAuth views requires Django session support" "from django.http Import Httpresponseredirectfrom weibopy Import Oauthhandler, OAuth, Weiboperrorconsumer_key = ' # Set the Appkeyconsumer_secret you applied for = ' # Set the appkey you apply for 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 Reque St Token self.request_token = Self._get_request_token () # Build Auth request and return as URL if Signin_wi      Th_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 = '/' # to avoid outbound jumps to the login page and jump errors occur False return Referer_urldef _oauth (): "" "Get OAuth Authentication Class" "" Return Weboauthhandler (Consumer_key, Consumer_secret) def login (requ EST): # Save the original login URL so that the authentication succeeds after jumping 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_c  Lient.get_authorization_url_with_callback (login_backurl) # Save Request_token, you need to use it to get access_token after login request.session[' Oauth_request_token ' = auth_client.request_token # jump to login page return Httpresponseredirect (Auth_url) def login_check (Request): "" "After the user successfully signed in to authorize, will callback this method, get Access_token, complete Authorization" "" "" "# http://mk2.com/?oauth_token= c30fa6d693ae9c23dd0982dae6a1c5f9&oauth_verifier=603896 verifier = Request. Get.get (' Oauth_verifier ', None) auth_client = _oauth () # Set the Request_token request before saving in session_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, later access just use Access_token can 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): "" "User logout, Directly delete Access_token "" "Del request.session[' Oauth_access_token ') Back_to_url = _get_referer_url (Request) return Httpresponseredirect (Back_to_url)

Hopefully this article will help you with Python programming.

  • Related Article

    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.