当前位置:网站首页>Wechat open platform scanning code login [easy to understand]
Wechat open platform scanning code login [easy to understand]
2022-07-01 22:37:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm your friend, Quan Jun .
WeChat The function of scanning code and logging in on the open platform can only be used by the authenticated wechat official account , Many students who study wechat may not be able to use this function , But don't worry , There are many accounts available in the following website :
Application 1 : apply name : do not say or talk AppID:wx2a6cf9373f278ed2 Appsecret:b32af04d9062ebc44b3f5d8b87a5a48e Callback the domain name configuration :testxdclass.tunnel.qydev.com |
|---|
Application 2 : apply name : Wolf Road AppID:wx025575eac69a2d5b Appsecret:72024309d9751d33e97e9d2a66e89944 Callback the domain name configuration :test16web.tunnel.qydev.com |
|---|
The first problem is that the domain name of the project is inconsistent with the wechat authorized domain name , There is no way to set your domain name to , For example, my project domain name is t238997p11.qicp.vip , So what I hope is , When accessing the domain name authorized by the wechat background , It can automatically jump to my own domain name , So you can run the background code .
———————————————————————————————————————–
Take my own account number as an example :
openAppId: wx277996c4d2772104 # Open platform appid
openAppSecret: xxxxxxxxxxxxxxxxxxxxxxxxxxx # Development platform appSecret
wechatOpenAuthorize: testdev.tunnel.qydev.com # Callback domain name One 、 First step : install nginx Acting as agent
1、 Download and install nginx : Official website :http://nginx.org/en/download.html
Click to enter the official website , Download the stable version
After decompression, click nginx.exe, Open Task Manager , See the following illustration nginx Enabled
2、 utilize nginx Acting as agent , Make when access testdev.tunnel.qydev.com When authorizing the domain name , Can access my own background code .
namely , When accessing http://testdev.tunnel.qydev.com 【80 The port is omitted and not written 】 Is equivalent to accessing 127.0.0.1:8080
(1) To configure nginx.conf
Be careful :A:proxy_pass The agent is upstream The content in , also upstream The following name must be consistent with proxy_pass Consistence after
B: The listening port number must be 80, If 80 The port has been occupied. Be sure to release it first
C:server_name It is the authorized domain name of wechat
upstream sell { server 127.0.0.1:8080; }
server { listen 80; server_name testdev.tunnel.qydev.com;
location / { proxy_pass http://sell; }
}
(2) modify hosts file ( stay C:\Windows\System32\drivers\etc Under the table of contents )
add to :127.0.0.1 testdev.tunnel.qydev.com
Two 、SpringBoot Background code
@Controller
@RequestMapping("/wechat")
@Slf4j
public class WechatController {
@Autowired
private WxMpService wxOpenService;
/**
* Wechat open platform authorized login
*
* @param returnUrl
* @return
*/
// visit :http://127.0.0.1:8080/sell/wechat/qrAuthorize?returnUrl=http://www.imooc.com
@GetMapping("/qrAuthorize")
public String qrAuthorize(@RequestParam("returnUrl") String returnUrl){
String url = "http://testdev.tunnel.qydev.com/sell/wechat/qrUserInfo";
String redirectUrl = wxOpenService.buildQrConnectUrl(url,WxConsts.QrConnectScope.SNSAPI_LOGIN, URLEncoder.encode(returnUrl));
log.info(" Wechat open platform authorization code,redirectUrl={}",redirectUrl);
return "redirect:" + redirectUrl;
}
/**
* Wechat open platform scanning code to get openid
*
* @param code
* @param returnUrl
* @return
*/
@GetMapping("/qrUserInfo")
public String qrUserInfo(@RequestParam("code") String code,
@RequestParam("state") String returnUrl){
WxMpOAuth2AccessToken wxMpOAuth2AccessToken = new WxMpOAuth2AccessToken();
try {
wxMpOAuth2AccessToken = wxOpenService.oauth2getAccessToken(code);
} catch (WxErrorException e) {
log.info("[ WeChat web page authorization ] {}",e);
throw new SellException(ResultEnum.WECHAT_MP_ERROR.getCode(),e.getError().getErrorMsg());
}
// obtain openid
String openId = wxMpOAuth2AccessToken.getOpenId();
return "redirect:" + returnUrl+"?openid=" + openId;
}
}3、 ... and 、 result
visit : http://127.0.0.1:8080/sell/wechat/qrAuthorize?returnUrl=http://www.imooc.com
As shown in the figure below , The address bar changes to :https://open.weixin.qq.com/connect/qrconnect?appid=wx277996c4d2772104&redirect_uri=http%3A%2F%2Ftestdev.tunnel.qydev.com%2Fsell%2Fwechat%2FqrUserInfo&response_type=code&scope=snsapi_login&state=http%3A%2F%2Fwww.imooc.com#wechat_redirect
After scanning the code, wechat jumps to returnUrl Callback address after http://www.imooc.com, And back to openid
The address bar changes to https://www.imooc.com/?openid=XXXXXJGsKKpEqGMEt7V7XXXXXX
According to the above steps, wechat scanning code login is realized !! If you have any questions, please give me advice !!
Wechat open platform reference manual :https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&lang=zh_CN
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/130289.html Link to the original text :https://javaforall.cn
边栏推荐
- Unity 使用Sqlite
- LC501. 二叉搜索树中的众数
- 功能测试报告的编写
- Why does blocprovider feel similar to provider?
- Qtreeview+qabstractitemmodel custom model: the third of a series of tutorials [easy to understand]
- Learn MySQL from scratch - database and data table operations
- Classify boost libraries by function
- 比较版本号[双指针截取自己想要的字串]
- "The silk road is in its youth and looks at Fujian" is in the hot collection of works in the Fujian foreign youth short video competition
- EasyExcel 复杂数据导出
猜你喜欢

MySQL series transaction log redo log learning notes

C#/VB.NET 给PDF文档添加文本/图像水印

flink sql-client 使用 对照并熟悉官方文档

Fully annotated SSM framework construction

Basic operation of binary tree
![比较版本号[双指针截取自己想要的字串]](/img/19/4f858ffdc1281d6b8b18a996467f10.png)
比较版本号[双指针截取自己想要的字串]

mysql 学习笔记-优化之SQL优化

Mysql——》MyISAM存储引擎的索引

Icml2022 | interventional contrastive learning based on meta semantic regularization

配置筛选机
随机推荐
互联网的智算架构设计
"The silk road is in its youth and looks at Fujian" is in the hot collection of works in the Fujian foreign youth short video competition
Communication between browser tab pages
spark analyze命令使用及其作用 map join broadcast join 广播join
The second anniversary of the three winged bird: the wings are getting richer and the take-off is just around the corner
Sonic cloud real machine learning summary 6 - 1.4.1 server and agent deployment
[STM32] stm32cubemx tutorial II - basic use (new projects light up LED lights)
Can you get a raise? Analysis on gold content of PMP certificate
Mysql——》Innodb存储引擎的索引
Yyds dry goods inventory # solve the real problem of famous enterprises: egg twisting machine
从零开始学 MySQL —数据库和数据表操作
Recent public ancestor (LCA) online practices
MySQL learning notes - SQL optimization of optimization
plantuml介绍与使用
LC669. 修剪二叉搜索树
[live broadcast review] the first 8 live broadcasts of battle code Pioneer have come to a perfect end. Please look forward to the next one!
信标委云原生专题组组长,任重道远!
20220701
Training on the device with MIT | 256Kb memory
CSDN购买的课程从哪里可以进入