Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Django to realize openid login authentication of Wechat official account users

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

How to use Django to achieve Wechat official account user openid login authentication, in view of this problem, this article introduces the corresponding analysis and answer in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

Recently, I am doing a small project with Django, which needs to associate the users of Wechat with the users of the site. Because Subscription account of Wechat does not have the authority to authorize the oauth page, we can only choose the second place to obtain the user's openid in the response to uniquely identify the user.

The user model in Django inherits and extends from AbstractUser, adding the openid field (models.py) to the user model:

Class Users (AbstractUser): openid = models.CharField (max_length=100,blank=True,null=True,verbose_name= "openid", unique=True)

We extend the user's model and use this model as the user authentication model. We need to specify the authentication model in the setting.py file (website is the name of the django application, not the project name):

AUTH_USER_MODEL = 'website.Users'

In this way, we can use the Users model defined above for user login and registration operations.

A common default Django login authentication, which uses authenticate, here refers to the description in the Django document:

To authenticate a given user name and password, use authenticate ()

It receives credentials in the form of keyword parameters, and for default configuration it is username

And password, which returns a User object if the password is valid for a given user name.

If the password is not valid, authenticate () returns None.

Example:

From django.contrib.auth import authenticate user = authenticate (username='john', password='secret') if user is not None: if user.is_active: print ("User is valid, active and authenticated") else: print ("The password is valid, but the account has been disabled!") Else: print ("The username and password were incorrect.")

If authenticate returns the correct User object, we log in to the returned User object using the login () method:

From django.contrib.auth import loginlogin (request,user)

This completes the most basic Django user authentication.

If we want to use other ways of login authentication, such as email address, mobile number, or the key point of this article: Wechat openid, then we need to customize the authentication method.

It is very convenient to perform custom authentication in Django. It only takes three steps to complete a custom authentication:

1. Write an authentication backend:

An authentication backend is a class that implements two methods: get_user (user_id) and authenticate (* * credentials)

Here, we create a new py file wechatAuth.py to write the authentication backend of openid:

From. Models import Users''' Wechat openid authentication login''class WechatOpenidAuth (object): def get_user (self,id_): try:

Return Users.objects.get (pk=id_)

Except Users.DoesNotExist:

Return None def authenticate (self,openid=None): try: user = Users.objects.get (openid=openid)

If user is not None:

Return user

Else:

Return None except Users.DoesNotExist:

Return None

2. Specify the authentication backend in the configuration file setting.py:

At the bottom, Django maintains a list of "authentication backend".

When django.contrib.auth.authenticate () is called, Django attempts to authenticate all the authentication backgrounds.

If the first authentication method fails, Django will try the second, and so on, until all authentication backgrounds are tried.

The authentication background used is specified by the AUTHENTICATION_BACKENDS setting.

AUTHENTICATION_BACKENDS = ('django.contrib.auth.backends.ModelBackend'

'website.wechat_auth.WechatOpenidAuth',)

The first authentication backend is the default authentication method of Django, because it still needs to be used on the web side, so it is retained, and the second is the openid-based authentication backend.

3. Use the custom authentication backend to process login authorization:

We also use the authenticate () method and the login () method, but we pass in only one parameter, openid.

From django.contrib.auth import login,authenticatedef auth (request,openid): try: auth = authenticate (openid=openid) login (request,auth) print ("login successful", auth)

Except Exception as e: print (e)

In this way, an openid-based authentication is completed.

In Subscription account on Wechat, we can use the click event to return a text message or a picture-text message with the parameter of openid in its link. In this way, when the user clicks the link, the user's login can be completed silently.

This is the answer to the question about how to use Django to achieve the openid login authentication of Wechat official account users. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report