程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Django wechat login local test

編輯:Python

Today, I want to test the wechat login , How to fix it , Yes appId, Have a secret key , But how to fix it , Let's sort out .

1. Register first Wechat public platform test number

http://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index

Scan wechat , You can get a test number .

2. With the test number , You can see appID and appsecret La .

next step , Slide slide to Test number QR code , Scan and add your own micro signal .

Next step , Experience interface permission table , Slide directly to Web Services - Web account , Click Modify .

Will ask you to need one Domain name of authorization callback page .

3. How to fill in the domain name of this authorization callback page

Because we test locally , We modify it directly hosts Just the documents .

  • First step , open hosts file ,MAC System :sudo vim /etc/hosts Just fine .
  • The second step , Fill in directly 127.0.0.1 weixintest.com, I picked it up .
  • The third step , preservation , sign out , Then the domain name of the authorization callback page is filled in weixintest.com

4. Next you need to look at the documentation , How to log in via wechat .

Here is the wechat development document

https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html#0

Paste part of the document directly

1 First step : User consent authorization , obtain code

2 The second step : adopt code Website authorization access_token

3 The third step : Refresh access_token( if necessary )

4 Step four : Pull the user information ( Need to be scope by snsapi_userinfo)

5 attach : Inspection authorization certificate (access_token) Whether it works

4.1 How are we going to do ?

  • First step : Download wechat developer tools , Top left menu , Change the developer mode to The official account web page debugged
  • The second step : Request connection in wechat developer tool :https://open.weixin.qq.com/connect/oauth2/authorize?appid=OPENID&redirect_uri=REDIRCT_URI&response_type=code&scope=snsapi_base&state=123#wechat_redirect
  • Take what we have got openid, Fill it in and , Fill in our redirected uri That's it .
4.2 The redirection of url How to do it ?

We directly Django Just write a view .

appid = 'xxx'
secret = 'xxxx'
code = request.GET.get('code')
weixin_url = requests.get(
f"https://api.weixin.qq.com/sns/oauth2/access_token?appid={appid}&secret={secret}&code={code}&grant_type=authorization_code")
info = weixin_url.json()
open_id = info.get('openid')
access_token = info.get('access_token')
user_info_req = requests.get(
f'https://api.weixin.qq.com/sns/userinfo?access_token={access_token}&openid={open_id}&lang=zh_CN')
user_info_req.encoding = 'utf-8'
user_info = user_info_req.json()
context = {'code': request.GET.get('code'), 'info': info, 'user_info': user_info}
return render(request, 'test.html', context)

This is a simple view , The data we obtained is rendered to test.html On , Facilitate our intuitive observation .

Last in urls.py On , Define it url, such as /test/ And so on. .

Don't forget to start python manage.py 127.0.0.1:80, Yes, what you need to open is 80 port , Not other ports .

Finally, our redirected domain name is weixintest.com/test/ , In fact, that is 127.0.0.1:80/test/.

4.3 test.html Content , Simple rendering
<html lang="en">
<head>
<meta charset="UTF-8">
<title> test </title>
</head>
<body>
<h3>{
{ code }}</h3>
{% for k,v in info.items%}
<p>{
{k}}:{
{ v }}</p>
{% endfor %}
{% for k,v in user_info.items %}
<p>{
{k}}:{
{v}}</p>
{% endfor %}
</body>
</html>

Finally, you just need to input the above words in the wechat developer tool url, Will redirect us weixintest.com/test/ On the route , Render some of our simple data .

The same is true for subsequent tests .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved