网页授权获取用户基本信息:两种 scope 域
https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope={2}&state=ok#wechat_redirect
snsapi_base 不需要用户点同意,直接跳转到授权后的页面,只能用于获取openid,不能获取用户基本信息
snsapi_userinfo 会征求用户同意,授权后,可以获取用户基本信息
一开始以为问题出现在获得code阶段,增加a=1与code判断都不行,后来查看日志发现
{"errcode":48001,"errmsg":"api unauthorized, hints: [ req_id: PwlxNA02912289 ]"}
检查源代码后,将
"http://www.xxxxxx.com/wxauth/get-weixin-code.html?" +
"appid=" + wx_appid +
"&scope=snsapi_base&state=&redirect_uri="
中的snsapi_base改成snsapi_userInfo
问题解决 需要授权才能查看公众号内容
使用 scope=snsapi_userinfo,用户同意授权后,access_token 的有效期是7200秒(两小时)。
access_token 失效后,可以使用 refresh_token 调用接口 https://api.weixin.qq.com/sns/oauth2/refresh_token?appid={0}&grant_type=refresh_token&refresh_token={1} 重新获取 access_token(有效期7200秒)
refresh_token 的有效期是30天,即用户同意授权后,在30天内,不再需要用户同意授权,就可以获取用户基本信息
前置与注意转自 https://blog.csdn.net/zhanghao143lina/article/details/78994397