公众号获取openid流程如下:
一、首先登录进入公众平台,点击【公众号设置】,然后再点击【网页授权域名】,授权后进行第二步
二、自定义菜单
调自定义菜单创建接口:如
{
"button": [
{
"type": "click",
"name": "click",
"key": "V1001_TODAY"
},
{
"name": "菜单",
"sub_button": [
{
"type":"view",
"name": "test",
"url":"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxc56301d738e5d770&redirect_uri=train.gome.com.cn&response_type=code&scope=snsapi_base&state=123#wechat_redirect"
},
{
"type":"view",
"name":"test跳转",
"url":"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx520c15f417810387&redirect_uri=https%3A%2F%2Fchong.qq.com%2Fphp%2Findex.php%3Fd%3D%26c%3DwxAdapter%26m%3DmobileDeal%26showwxpaytitle%3D1%26vb2ctag%3D4_2030_5_1194_60&response_type=code&scope=snsapi_base&state=123#wechat_redirect"
}
]
}
]
}
其中url中redirect_uri是该该菜单要跳转的链接页面。在该页面
获取code后,请求以下链接获取access_token: https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
参数说明
参数 | 是否必须 | 说明 |
appid | 是 | 公众号的唯一标识 |
secret | 是 | 公众号的appsecret |
code | 是 | 填写第一步获取的code参数 |
grant_type | 是 | 填写为authorization_code |
返回说明
正确时返回的JSON数据包如下:
{ "access_token":"ACCESS_TOKEN",
"expires_in":7200,
"refresh_token":"REFRESH_TOKEN",
"openid":"OPENID",
"scope":"SCOPE" }
参数 | 描述 |
access_token | 网页授权接口调用凭证,注意:此access_token与基础支持的access_token不同 |
expires_in | access_token接口调用凭证超时时间,单位(秒) |
refresh_token | 用户刷新access_token |
openid | 用户唯一标识,请注意,在未关注公众号时,用户访问公众号的网页,也会产生一个用户和公众号唯一的OpenID |
scope | 用户授权的作用域,使用逗号(,)分隔 |
错误时微信会返回JSON数据包如下(示例为Code无效错误):
{"errcode":40029,"errmsg":"invalidcode"}