获取微信基本信息简单几步骤
1.使用微信授权链接。
请求地址:
https://open.weixin.qq.com/connect/oauth2/authorize?appid=【appid】&redirect_uri=http://www.rcoauth2.cn/oauth2.aspx&response_type=code&scope=snsapi_userinfo&state=1&connect_redirect=1#wechat_redirect
参数解释:
redirect_uri,配置后的回调地址,自己的
appid,申请公众号都有
scope,授权方式,一般写snsapi_userinfo。还有snsapi_base默认授权(一般公众号没权限)
回调后得到code:
http://www.rcoauth2.cn/oauth2.aspx?code=081mJnFo0kjDoo1I6BF10TGkFo0mJnFA&state=1
2.根据步骤1得到的code请求到access_token和用户的openid
请求地址:
https://api.weixin.qq.com/sns/oauth2/access_token?appid=【appid】&secret=【AppSecret】&code=081mJnFo0kjDoo1I6BF10TGkFo0mJnFA&grant_type=authorization_code
请求结果:
{
"access_token":"【access_token】",
"expires_in":7200,
"refresh_token":"Rujq37gAZDBUBNFiBEAXz-vO9Sl6ALV47jTZJfWUaaa1pGT6BFasdP3BFmmlei0jBPmkQAqAyjwblsmI3nnhxiarAsLgfRGZ_ZFnNNSrlPc",
"openid":“【openid】",
"scope":"snsapi_userinfo"
}
3.根据步骤2获取的access_token 和 openid就可以获取到头像信息等用户基本信息
请求地址:
https://api.weixin.qq.com/sns/userinfo?access_token=【access_token】&openid=【openid】
请求结果:
{
"openid":"【openid】",
"nickname":”Chuan",
"sex":1,
"language":"zh_CN",
"city":"",
"province":"",
"country":"AD",
"headimgurl":"http://wx.qlogo.cn/mmopen/vi_32/ico2IiamJ5kVonQx0n0qd3ibDv4UeTLOtibjmSwxLsGmDzqk2HJH9icpsyK8mhCG24xfeW200kTT5AnzI7ATFMM30KQ/0",
"privilege”:[
]
}