微信小程序选项卡功能开发步骤与方法全解析
151
2024-07-29
在这个移动端的时代,微信公众号也逐渐成为人们获取信息的一种渠道,也是商家发展潜在客户的一种手段,因此,许多程序员转战微信开发,那么今天,我们就来为大家介绍一下微信公众号开发中的网页授权到底要怎样才能实现。
在这之前先给大家一个我自定义的请求接口的函数,在下面的示例代码中请求接口用的都是这个函数
该函数的作用是,想接口发起请求,传递参数并返回接口返回的数据
(这个里面的代码就不做多解释了,如果大家想要了解可以去看一下php curl函数总结)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
(文中所使用的接口为腾讯官方提供,大家可以参考一下微信公众平台的开发者文档)
一、首先我们需要配置我们的公众号
1、在微信公众号请求用户网页授权之前,开发者需要先到公众平台官网中的“开发 - 接口权限 - 网页服务 - 网页帐号 - 网页授权获取用户基本信息”的配置选项中,修改授权回调域名。请注意,这里填写的是域名(是一个字符串),而不是URL,因此请勿加 http:// 等协议头;
2、授权回调域名配置规范为全域名,比如需要网页授权的域名为:www.qq.com,配置以后此域名下面的页面http://www.qq.com/music.html 、 http://www.qq.com/login.html 都可以进行OAuth2.0鉴权。但http://pay.qq.com 、 http://music.qq.com 、 http://qq.com无法进行OAuth2.0鉴权
3、如果公众号登录授权给了第三方开发者来进行管理,则不必做任何设置,由第三方代替公众号实现网页授权即可
二、用户同意授权,获取code
接口地址:https://open.weixin.qq.com/connect/oauth2/authorize?APPid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect (注意接口参数)
1 2 3 4 5 6 7 8 |
|
三、通个获取到的code来或缺access_token和openid
接口:https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
1 2 3 4 5 6 7 8 9 10 11 |
|
四、判断access_token是否有效
接口:https://api.weixin.qq.com/sns/auth?access_token=ACCESS_TOKEN&openid=OPENID
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
五、如果失效,刷新access_token
接口:https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=APPID&grant_type=refresh_token&refresh_token=REFRESH_TOKEN
1 2 3 4 5 6 7 8 9 10 |
|
六、获取用户信息
接口:https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN
1 2 3 4 5 6 |
|
获取到用户信息数据:
1 2 3 4 5 6 7 8 9 10 11 |
|
下面上完整代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
|
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~