今天完成的事情:
完成微信登陆认证流程, 通过opeonId 和 access_token 获取用户信息
微信公众号获取用户信息
1. 获取code, 必须在微信开发工具或者微信中打开才会跳转
- redirect_uri必须填写网页授权域名, 公众号设置>功能设置>网页授权域名
https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx2750055a558bbe86&redirect_uri=http://academy.home.canary.dounixue.net/&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect
- 跳转后url
http://academy.home.canary.dounixue.net/?code=001oDwYl1bI7al0xsVXl1AqTYl1oDwYD&state=STATE
2. 通过code以及appid/secret 获取 access_token
- 通过code获取access_token, wx2750055a558bbe86/fe33aae20890da44fc14c709468b7a91
https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx2750055a558bbe86&secret=fe33aae20890da44fc14c709468b7a91&code=0815xeel0JaaOl1HRxfl06bcel05xee7&grant_type=authorization_code
- 返回结果:
1
2
3
4
5
6
7{
"access_token": "12_Y7wwnaQ8VA5joJfIEcZSKtO8h5Omrvn80be6nb18jG_9Ad1YetJDEEwCLpnQqaNcq1JlUVuZoXvPsu0QPJ73TA",
"expires_in": 7200,
"refresh_token": "12_4jUAxLbsJ-H_Tcu9A4EJr7CxNcvQZrMwbtokLVdfXUnionm9aRXdDY3fEX-ZkhfeTzQfl9rgt2kz3FbTefWV7g",
"openid": "oSV35t_fiRPKozt9JV4kQD-O5XMg",
"scope": "snsapi_userinfo"
}
3. 刷新access_token(如果需要)
由于access_token拥有较短的有效期,当access_token超时后,可以使用refresh_token进行刷新,refresh_token有效期为30天,当refresh_token失效之后,需要用户重新授权。https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=wx2750055a558bbe86&grant_type=refresh_token&refresh_token=12_4jUAxLbsJ-H_Tcu9A4EJr7CxNcvQZrMwbtokLVdfXUnionm9aRXdDY3fEX-ZkhfeTzQfl9rgt2kz3FbTefWV7g
- 返回结果:
1
2
3
4
5
6
7{
"openid": "oSV35t_fiRPKozt9JV4kQD-O5XMg",
"access_token": "12_Y7wwnaQ8VA5joJfIEcZSKtO8h5Omrvn80be6nb18jG_9Ad1YetJDEEwCLpnQqaNcq1JlUVuZoXvPsu0QPJ73TA",
"expires_in": 7200,
"refresh_token": "12_4jUAxLbsJ-H_Tcu9A4EJr7CxNcvQZrMwbtokLVdfXUnionm9aRXdDY3fEX-ZkhfeTzQfl9rgt2kz3FbTefWV7g",
"scope": "snsapi_base,snsapi_userinfo,"
}通过openid 和 access_token 获取用户基本信息
https://api.weixin.qq.com/sns/userinfo?access_token=12_iMk8sNzOKW_jN6qpZrw8PwP4NKApKVaf40PojX_w7I49kqod3Eorwk0OhG-BDcow4d2a4wgtQAbcK5GDzhWd8Q&lang=zh_CN - 返回结果:
1
2
3
4
5
6
7
8
9
10
11{
"openid": "oSV35t_fiRPKozt9JV4kQD-O5XMg",
"nickname": ".",
"sex": 1,
"language": "zh_CN",
"city": "",
"province": "上海",
"country": "中国",
"headimgurl": "http://thirdwx.qlogo.cn/mmopen/vi_32/PiajxSqBRaEJibfyYTuSD3nIVDfhkyKHy4NdfrF5ibQDSicW4ocn3Jw8W8NkwLpcjpQWKZuV7YSMHtQGCCBOAeGXXg/132",
"privilege": []
}
明天计划的事:
完成微信demo, 完善方案设计
https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN
https://api.weixin.qq.com/sns/userinfo?access_token=12_BKHVHYjgA18AawXYiPOzKwOR3ZGUBnsLmiUrPLbI9MePuPNw1b4NXSpt8qwIUTBxIiudSOjOme81qxABhXhUew&openid=oSV35t_fiRPKozt9JV4kQD-O5XMg