微信登录授权流程
1、准备工作,需要在微信开放平台申请一个应用,需要网站域名备案过。
得到:appid和secret两个重要参数,这边域名涉及到后面微信回调。
2、时序图
3、主要步骤四点
一)根据appid和回调redirect_uri生成二维码图
https://open.weixin.qq.com/connect/qrconnect?appid=wx410c26fd9ec1a11e&redirect_uri=http://www.xiangxueketang.cn/wx/callback&response_type=code&scope=snsapi_login&state=STATE#wechat_redirect
二)用户扫完二维码图,确认后,微信会回调redirect_uri对应的地址,把code传过来
三)根据code,appid,secret换取token
https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
返回结果
{ "access_token":"ACCESS_TOKEN", "expires_in":7200, "refresh_token":"REFRESH_TOKEN", "openid":"OPENID", "scope":"SCOPE", "unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL" }
四)根据token得到用户的信息
https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID
返回结果:
{ "openid":"OPENID", "nickname":"NICKNAME", "sex":1, "province":"PROVINCE", "city":"CITY", "country":"COUNTRY", "headimgurl": "https://thirdwx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/0", "privilege":[ "PRIVILEGE1", "PRIVILEGE2" ], "unionid": " o6_bmasdasdsad6_2sgVt7hMZOPfL" }