How django connects to Sina Weibo OAuth

Source: Internet
Author: User
Tags oauth
This article mainly introduces django's method of accessing Sina Weibo OAuth, and analyzes django's access skills for Sina Weibo OAuth interface, for more information about how django connects to Sina Weibo OAuth, see the example in this article. Share it with you for your reference. The specific analysis is as follows:

Recently, I have integrated my website with Sina Weibo. The idea is very simple: to associate the page content with Sina Weibo, a page with an independent content for a microblog, the natural comment system only needs Weibo comments. Then, if a user needs to post a comment, he must access oauth. cannot He log on to your website to send a comment? No one will tell you your account and password. View the authorization mechanism of Sina Weibo and download the python sdk to access oauth on django.

If you are unfamiliar with oauth, first check the OAUTH protocol introduction.

In fact, the process is very simple:

① Getrequesttoken->
② Createauthurl->
③ [User_login: Jump to the Sina login page, and the user will jump back after login]->
④ Getaccesstoken->
⑤ Done!

The following is a detailed description of the specific implementation code of the python sdk on django:

The oauth_views.py file is as follows:

#! /Usr/bin/env python #-*-coding: UTF-8-*-"django-based Sina Weibo oauth views requires django's session support" from django. http import HttpResponseRedirectfrom weibopy import OAuthHandler, oauth, authorization = ''# Set Your applied appkeyconsumer_secret ='' # set secretclass WebOAuthHandler (OAuthHandler) for the appkey you applied ): 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 ('authorization') request = oauth. OAuthRequest. from_token_and_callback (token = self. request_token, callback = callback, http_url = url) return request. to_url () does T 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 redirection errors when the external site jumps directly to the logon page. return referer_urldef _ oauth (): "get oauth Authentication class" "return WebOAuthHandler (consumer_key, consumer_secret) def login (request): # save the initial logon url so that the back_to_url = _ get_referer_url (request) request is returned after authentication is successful. session ['login _ back_to_url '] = back_to_url # obtain the oauth url login_backurl = request. build_absolute_uri ('/login_check') auth_client = _ oauth () auth_url = token (login_backurl) # save request_token. you need to use it to obtain the access_token request after logging on. session ['Oss _ request_token '] = auth_client.request_token # go to the logon page return HttpResponseRedirect (auth_url) def login_check (request): "This method is called back after a user successfully logs on to the authorization page, get access_token and complete authorization """# http://mk2.com/?oauth_token=c30fa6d693ae9c23dd0982dae6a1c5f9&oauth_verifier=603896 Verifier = request. GET. get ('Oss _ verifier ', None) auth_client = _ oauth () # set request_token = request saved in the session. session ['Oss _ request_token'] del request. session ['Oss _ request_token '] requests (request_token.key, request_token.secret) access_token = token (verifier) # save access_token. you only need to use access_token for future access. session ['Oss _ access_token '] = access_token # jump back to the page before initial logon back_to_url = request. session. get ('login _ back_to_url ','/') return HttpResponseRedirect (back_to_url) def logout (request): "the user logs out and deletes access_token" del request. session ['Oss _ access_token'] back_to_url = _ get_referer_url (request) return HttpResponseRedirect (back_to_url)

I hope this article will help you with 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.