100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > 微信公众号授权登录网页获取code

微信公众号授权登录网页获取code

时间:2018-09-16 13:37:12

相关推荐

微信公众号授权登录网页获取code

配置回调函数

我们在微信客户端访问第三方网页(即我们自己的网页)的时候,我们可以通过微信网页授权机制,我们不仅要有前面获取到的appid和appsecret还需要有当用户授权之后,回调的域名设置,即用户授权后,页面会跳转到哪里。具体的配置如下:

请求授权页面的构造方式:

https://open./connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect

应用授权作用域:由于snsapi_base只能获取到openid,意义不大,所以我们使用snsapi_userinfo。参考链接如下:(域名是你配置的回调函数域名)>https://open./connect/oauth2/authorize?appid=wx3d0b47a2e903d8&redirect_uri=https://域名/wechattransfer.html&response_type=code&scope=snsapi_userinfo&state=https://域名/index.html#wechat_redirect`https://域名/wechattransfer.html`是授权后会跳转的地址,在这个页面中会解析路径参入从而进入`https://域名/index.html`([wechattransfer下载链接](/klover2/wechattransfer))下载wechattransfer.html之后配置在你的服务器上,在微信授权下的域名能够访问到`https://域名/index.html`是要进入的页面使用微信开发工具访问上面参考链接,最后会进入下面链接,同时带上codehttps://域名/index.html?code=0014rK302cHWcV0w7K502IhK3024rK3u

把当前code发送给服务器

const got = require('got');(async()=>{let appid= ''; // 公众号appidlet appsecret='';// 公众号appsecretlet WxLoginInfo = (await got.get(`https://api./sns/oauth2/access_token?appid=${appid}&secret=${appsecret}&code=${code}&grant_type=authorization_code`, {'json': true })).body;// 获取微信中的用户信息let {access_token, openid } = WxLoginInfo || {};// 通过微信获取用户信息let wxUserInfo = (await got.get(`https://api./sns/userinfo?access_token=${access_token}&openid=${openid}`, {'json': true })).body;})()

注意:

使用微信调试工具调试必须得有当前公众号开发者权限如何添加

UnionID机制的作用说明:如果开发者拥有多个移动应用、网站应用和公众帐号,可通过获取用户基本信息中的unionid来区分用户的唯一性,因为同一用户,对同一个微信开放平台下的不同应用(移动应用、网站应用和公众帐号),unionid是相同的。

文档

微信v3版支付

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。