当前位置:网站首页>微信第三方网页授权
微信第三方网页授权
2022-04-22 06:10:00 【sunshine641】
官方文档可以看上边。
主要步骤

一、获取code
https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect
引导用户点开上面的链接,向微信请求授权获取code
主要修改 APPID、REDIRECT_URI、SCOPE 这三个值

REDIRECT_URI 指的是你获取到授权后,要跳转到的静态界面(比如首页)。
这里一定要对url进行编码,不然会报错
const appId = 'xx 你的AppID xx';
const redirectUri = encodeURIComponent('http://你要跳的页面/');
const url = `https://open.weixin.qq.com/connect/oauth2/authorize?
appid=${appId}&redirect_uri=${redirectUri}&response_type=code
&scope=snsapi_userinfo&state=STATE#wechat_redirect`;
//scope选的第二种授权snsapi_userinfo
window.location.href = url
通过window.location.href 跳转到该链接后,微信会重定向返回到你给的指定的页面,后面会携带code、state等参数,
类似这样:http://你要跳的
页面/?code=xxx返给你的codexxx&state=STATE 。
然后我们截取code=后面的加粗字段,就可以用它去请求需要的数据了。
前端最好不要用http请求,会有跨域问题 徒增麻烦,用window.location.href 跳转就好。
截取我这里用indexof 查code= 和 &state 位置,然后substring()截取的,方法比较笨,大佬可以用正则写,欢迎分享。
我的项目只用到了第一条,所以记录到这里,其他的可以看官方文档,连接放在最上面。
版权声明
本文为[sunshine641]所创,转载请带上原文链接,感谢
https://blog.csdn.net/sunshine641/article/details/120868663
边栏推荐
- Minipcie interface can card expands can channel for industrial computer
- Leetcode 1218: Longest definite difference subsequence
- Comparison and improvement of Nacos service registration center
- Latex symbol and formula set
- 腾讯云对象存储服务的使用
- Digital IC design and CS?
- ASP.NET日常开发随手记------iis服务器支持下载apk
- common-net hp unix系统 ftp服务器 listFiles 返回为空解决方案。
- STM32 learning record 0004 - ISP serial port download
- Application of can card in robot system
猜你喜欢

Nacos持久化切换配置

Difference between analog IC design and digital IC design, including salary table

Td041s485h is fully compatible with iso3080, iso3086, iso3082 and iso3088

送给所有程序员的新年祝福&新年愿望

从 Spec.到芯片_(数字IC、模拟IC、FPGA/CPLD设计的流程及EDA工具)

Introduction to IC Analog Layout - learning notes on layout Basics (3)

集成电路模拟版图入门-版图基础学习笔记(一)

51单片机:D/A数模转换实验

How to become an IC Verification Engineer?

SQL学习记录
随机推荐
SQL学习记录
STM32 learning record 0002-stm32
VSCode打开小程序运行到微信开发者工具WXML 文件编译报错
CLion和动态链接库
Quantify PM2 5 data
实验室安全考试
Nacos服务消费者注册和负载均衡
Service configuration center of Nacos
51单片机:D/A数模转换实验
论文快速阅读的方法技巧
泛型与反射的实际使用练习(包含一个泛型缓存)----手写ORM框架
RSS入坑指南
Oracle JDK vs OpenJDK
Application of mcicecan in industrial computer
Activity支持多窗口显示
ASP.NET日常开发随手记------用文本文档记录日志
旋转选择器 WheelPicker 的使用
Application of can card in robot system
C daily development notes -- solve the problem that one set references another set, resulting in changes together
How to become an IC Verification Engineer?