微信公众平台的网页跳转时如何获取账号?
2022-12-08 · 百度认证:IT168官方账号,优质数码领域创作者
一、通过全局AccessToken获取用户基本信息
1.用户关注以及回复消息的时候,均可以获得用户的OpenID
<xml>
<ToUserName><![CDATA[gh_b629c48b653e]]></ToUserName>
<FromUserName><![CDATA[ollB4jv7LA3tydjviJp5V9qTU_kA]]></FromUserName>
<CreateTime>1372307736</CreateTime>
<MsgType><![CDATA[event]]></MsgType>
<Event><![CDATA[subscribe]]></Event>
<EventKey><![CDATA[]]></EventKey>
</xml>
其中的FromUserName就是OpenID
2.然后使用access_token接口,请求获得全局AccessToken
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
返回结果:
{
"access_token":"NU7Kr6v9L9TQaqm5NE3OTPctTZx797Wxw4Snd2WL2HHBqLCiXlDVOw2l-Se0I-WmOLLniAYLAwzhbYhXNjbLc_KAA092cxkmpj5FpuqNO0IL7bB0Exz5s5qC9Umypy-rz2y441W9qgfnmNtIZWSjSQ",
"expires_in":7200
}
3.再使用全局ACCESS_TOKEN获取OpenID的详细信息
https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID
返回如下:
{
"subscribe":1,
"openid":"oLVPpjqs2BhvzwPj5A-vTYAX4GLc",
"nickname":"方倍",
"sex":1,
"language":"zh_CN",
"city":"深圳",
"province":"广东",
"country":"中国",
"headimgurl":"http://wx.qlogo.cn/mmopen/JcDicrZBlREhnNXZRudod9PmibRkIs5K2f1tUQ7lFjC63pYHaXGxNDgMzjGDEuvzYZbFOqtUXaxSdoZG6iane5ko9H30krIbzGv/0",
"subscribe_time":1386160805
}
至此,获得用户的基本信息。
二、通过OAuth2.0方式弹出授权页面获得用户基本信息
1.首先配置回调域名
2.构造请求url如下:
https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx8888888888888888&redirect_uri=http://mascot.duapp.com/oauth2.php&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect
页面URL中的
scope=snsapi_userinfo表示应用授权作用域为请求用户信息
★如果使用别人的AppID和AppSecret,那么获得的OpenID是那个有高级接口权限的服务号的,这里可以通过消息回复,获取本公众账号下的OpenID,带入回调中,与另一个OpenID进行关联
https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx8888888888888888&redirect_uri=http://mascot.duapp.com/oauth2.php?userid=oc7tbuPA9BgUCLADib5nB3k2KWWg&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect
将该链接回复给关注用户,用户点击后,弹出应用授权界面
3.回调页面得到链接如下,回调url中将包含参数code
http://mascot.duapp.com/oauth2.php?code=00b788e3b42043c8459a57a8d8ab5d9f&state=1
或者http://mascot.duapp.com/oauth2.php?userid=oc7tbuPA9BgUCLADib5nB3k2KWWg&code=00b788e3b42043c8459a57a8d8ab5d9f&state=1
4.再使用code换取oauth2的授权access_token
url如下:
https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx8888888888888888&secret=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa&code=00b788e3b42043c8459a57a8d8ab5d9f&grant_type=authorization_code
获得授权AccessToken:
{
"access_token":"OezXcEiiBSKSxW0eoylIeAsR0GmYd1awCffdHgb4fhS_KKf2CotGj2cBNUKQQvj-G0ZWEE5-uBjBz941EOPqDQy5sS_GCs2z40dnvU99Y5AI1bw2uqN--2jXoBLIM5d6L9RImvm8Vg8cBAiLpWA8Vw",
"expires_in":7200,
"refresh_token":"OezXcEiiBSKSxW0eoylIeAsR0GmYd1awCffdHgb4fhS_KKf2CotGj2cBNUKQQvj-G0ZWEE5-uBjBz941EOPqDQy5sS_GCs2z40dnvU99Y5CZPAwZksiuz_6x_TfkLoXLU7kdKM2232WDXB3Msuzq1A",
"openid":"oLVPpjqs9BhvzwPj5A-vTYAX3GLc",
"scope":"snsapi_userinfo,"
}
5.再使用授权AccessToken获取用户信息
url如下:
https://api.weixin.qq.com/sns/userinfo?access_token=OezXcEiiBSKSxW0eoylIeAsR0GmYd1awCffdHgb4fhS_KKf2CotGj2cBNUKQQvj-G0ZWEE5-uBjBz941EOPqDQy5sS_GCs2z40dnvU99Y5AI1bw2uqN--2jXoBLIM5d6L9RImvm8Vg8cBAiLpWA8Vw&openid=oLVPpjqs9BhvzwPj5A-vTYAX3GLc
返回如下
{
"openid":"oLVPpjqs9BhvzwPj5A-vTYAX3GLc",
"nickname":"方倍",
"sex":1,
"language":"zh_CN",
"city":"Shenzhen",
"province":"Guangdong",
"country":"CN",
"headimgurl":"http://wx.qlogo.cn/mmopen/utpKYf69VAbCRDRlbUsPsdQN38DoibCkrU6SAMCSNx558eTaLVM8PyM6jlEGzOrH67hyZibIZPXu4BK1XNWzSXB3Cs4qpBBg18/0",
"privilege":[]
-
官方服务
- 官方网站
- 官方网站