您的位置 首页 php

微信公众号添加菜单栏和网页授权

关注了微信公众号怎么获取用户的信息以及 OpenID 和手机号的绑定

1.登录微信公众平台设置需要的菜单,以绑定微信账号为例

f799f39ac8a54c6fa0efd57fb5af277f

ebbc1498ca674b409e9f3fa79935d97e

2.网页地址填写自己接口地址或者跳转地址

3.后台接口(php 仅供参考)

5e63fbed3ac14643b9f768c68bcd1e84

两个红圈的地址是该接口地址,这个可以获取到用户的 openid ,下面的接口是用户的openid和手机号进行绑定

下面参考微信官方文档

如果用户在微信客户端中访问第三方网页,公众号可以通过微信网页授权机制,来获取用户基本信息,进而实现业务逻辑。

关于网页授权回调域名的说明

1、在微信公众号请求用户网页授权之前,开发者需要先到公众平台官网中的“开发 – 接口权限 – 网页服务 – 网页帐号 – 网页授权获取用户基本信息”的配置选项中,修改授权回调域名。请注意,这里填写的是域名(是一个 字符串 ),而不是URL,因此请勿加 http:// 等协议头;

2、授权回调域名配置规范为全域名,比如需要网页授权的域名为:www.qq.com,配置以后此域名下面的页面 、 都可以进行 OAuth2.0 鉴权。但 、 、 无法进行OAuth2.0鉴权

3、如果公众号登录授权给了第三方开发者来进行管理,则不必做任何设置,由第三方代替公众号实现网页授权即可

关于网页授权的两种scope的区别说明

1、以snsapi_base为scope发起的网页授权,是用来获取进入页面的用户的openid的,并且是静默授权并自动跳转到回调页的。用户感知的就是直接进入了回调页(往往是业务页面)

2、以snsapi_userinfo为scope发起的网页授权,是用来获取用户的基本信息的。但这种授权需要用户手动同意,并且由于用户同意过,所以无须关注,就可在授权后获取该用户的基本信息。

3、用户管理类接口中的“获取用户基本信息接口”,是在用户和公众号产生消息交互或关注后事件推送后,才能根据用户OpenID来获取用户基本信息。这个接口,包括其他微信接口,都是需要该用户(即openid)关注了公众号后,才能调用成功的。

关于网页授权access_token和普通access_token的区别

1、微信网页授权是通过OAuth2.0机制实现的,在用户授权给公众号后,公众号可以获取到一个网页授权特有的接口调用凭证(网页授权access_token),通过网页授权access_token可以进行授权后接口调用,如获取用户基本信息;

2、其他微信接口,需要通过基础支持中的“获取access_token”接口来获取到的普通access_token调用。

关于UnionID机制

1、请注意,网页授权获取用户基本信息也遵循UnionID机制。即如果开发者有在多个公众号,或在公众号、移动应用之间统一用户帐号的需求,需要前往微信开放平台(open.weixin.qq.com)绑定公众号后,才可利用UnionID机制来满足上述需求。

2、UnionID机制的作用说明:如果开发者拥有多个移动应用、网站应用和公众帐号,可通过获取用户基本信息中的unionid来区分用户的唯一性,因为同一用户,对同一个微信开放平台下的不同应用(移动应用、网站应用和公众帐号),unionid是相同的。

关于特殊场景下的静默授权

1、上面已经提到,对于以snsapi_base为scope的网页授权,就静默授权的,用户无感知;

2、对于已关注公众号的用户,如果用户从公众号的会话或者自定义菜单进入本公众号的网页授权页,即使是scope为snsapi_userinfo,也是静默授权,用户无感知。

具体而言,网页授权流程分为四步:

1、引导用户进入授权页面同意授权,获取code

2、通过code换取网页授权access_token(与基础支持中的access_token不同)

3、如果需要,开发者可以刷新网页授权access_token,避免过期

4、通过网页授权access_token和openid获取用户基本信息(支持UnionID机制)

目录

1 第一步:用户同意授权,获取code

2 第二步:通过code换取网页授权access_token

3 第三步:刷新access_token(如果需要)

4 第四步:拉取用户信息(需scope为 snsapi_userinfo)

5 附:检验授权凭证(access_token)是否有效

第一步:用户同意授权,获取code

在确保微信公众账号拥有授权作用域(scope参数)的权限的前提下(服务号获得高级接口后,默认拥有scope参数中的snsapi_base和snsapi_userinfo),引导关注者打开如下页面:

#wechat_redirect 若提示“该链接无法访问”,请检查参数是否填写错误,是否拥有scope参数对应的授权作用域权限。

尤其注意:由于授权操作安全等级较高,所以在发起授权请求时,微信会对授权链接做正则强匹配校验,如果链接的参数顺序不对,授权页面将无法正常访问

参考链接(请在微信客户端中打开此链接体验):

scope为snsapi_base

#wechat_redirect

scope为snsapi_userinfo

#wechat_redirect

尤其注意:跳转回调redirect_uri,应当使用https链接来确保授权code的安全性。

参数说明

参数

是否必须

说明

appid

公众号的唯一标识

redirect_uri

授权后重定向的回调链接地址, 请使用 urlEncode 对链接进行处理

response_type

返回类型,请填写code

scope

应用授权作用域,snsapi_base (不弹出授权页面,直接跳转,只能获取用户openid),snsapi_userinfo (弹出授权页面,可通过openid拿到昵称、性别、所在地。并且, 即使在未关注的情况下,只要用户授权,也能获取其信息 )

state

重定向后会带上state参数,开发者可以填写a-zA-Z0-9的参数值,最多128字节

#wechat_redirect

无论直接打开还是做页面302种定向时候,必须带此参数

下图为scope等于snsapi_userinfo时的授权页面:

2c4475fd92d9401bb7821e2deb21eae2

用户同意授权后

如果用户同意授权,页面将跳转至 redirect _uri/?code=CODE&state=STATE。

错误返回码说明如下:

返回码

说明

10003

redirect_uri域名与后台配置不一致

10004

此公众号被封禁

10005

此公众号并没有这些scope的权限

10006

必须关注此测试号

10009

操作太频繁了,请稍后重试

10010

scope不能为空

10011

redirect_uri不能为空

10012

appid不能为空

10013

state不能为空

10015

公众号未授权第三方平台,请检查授权状态

10016

不支持微信开放平台的Appid,请使用公众号Appid

第二步:通过code换取网页授权access_token

首先请注意,这里通过code换取的是一个特殊的网页授权access_token,与基础支持中的access_token(该access_token用于调用其他接口)不同。公众号可通过下述接口来获取网页授权access_token。如果网页授权的作用域为snsapi_base,则本步骤中获取到网页授权access_token的同时,也获取到了openid,snsapi_base式的网页授权流程即到此为止。

尤其注意:由于公众号的secret和获取到的access_token安全级别都非常高,必须只保存在服务器,不允许传给客户端。后续刷新access_token、通过access_token获取用户信息等步骤,也必须从服务器发起。

请求方法

获取code后,请求以下链接获取access_token:

参数说明

参数

是否必须

说明

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":"invalid code"}   

第三步:刷新access_token(如果需要)

由于access_token拥有较短的有效期,当access_token超时后,可以使用refresh_token进行刷新,refresh_token有效期为30天,当refresh_token失效之后,需要用户重新授权。

请求方法

获取第二步的refresh_token后,请求以下链接获取access_token:

参数

是否必须

说明

appid

公众号的唯一标识

grant_type

填写为refresh_token

refresh_token

填写通过access_token获取到的refresh_token参数

返回说明

正确时返回的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

用户唯一标识

scope

用户授权的作用域,使用逗号(,)分隔

错误时微信会返回JSON数据包如下(示例为code无效错误):

 {"errcode":40029,"errmsg":"invalid code"}   

第四步:拉取用户信息(需scope为 snsapi_userinfo)

如果网页授权作用域为snsapi_userinfo,则此时开发者可以通过access_token和openid拉取用户信息了。

请求方法

http:GET(请使用https协议)

参数说明

参数

描述

access_token

网页授权接口调用凭证,注意:此access_token与基础支持的access_token不同

openid

用户的唯一标识

lang

返回国家地区语言版本,zh_CN 简体,zh_TW 繁体,en 英语

返回说明

正确时返回的JSON数据包如下:

 {      "openid": "OPENID",   "nickname": NICKNAME,   "sex": 1,   "province":"PROVINCE",   "city":"CITY",   "country":"COUNTRY",   "headimgurl":"#34;,   "privilege":[ "PRIVILEGE1" "PRIVILEGE2"     ],   "unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL" }   

参数

描述

openid

用户的唯一标识

nickname

用户昵称

sex

用户的性别,值为1时是男性,值为2时是女性,值为0时是未知

province

用户个人资料填写的省份

city

普通用户个人资料填写的城市

country

国家,如中国为CN

headimgurl

用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空。若用户更换头像,原有头像URL将失效。

privilege

用户特权信息,json 数组,如微信沃卡用户为(chinaunicom)

unionid

只有在用户将公众号绑定到微信开放平台帐号后,才会出现该字段。

错误时微信会返回JSON数据包如下(示例为openid无效):

 {"errcode":40003,"errmsg":" invalid openid "}   

附:检验授权凭证(access_token)是否有效

请求方法

http:GET(请使用https协议)

参数说明

参数

描述

access_token

网页授权接口调用凭证,注意:此access_token与基础支持的access_token不同

openid

用户的唯一标识

返回说明 正确的JSON返回结果:

 { "errcode":0,"errmsg":"ok"}   

错误时的JSON返回示例:

 { "errcode":40003,"errmsg":"invalid openid"}  

文章来源:智云一二三科技

文章标题:微信公众号添加菜单栏和网页授权

文章地址:https://www.zhihuclub.com/36060.shtml

关于作者: 智云科技

热门文章

评论已关闭

32条评论

  1. Hospital officials say the turtle was likely inadvertently struck by a boat Consistent with this poor outcome association, the majority of cases 67 were assigned by the IRGS to the predicted high risk group Fig

  2. cialis efectos secundarios de metoprolol 25 mg In an effort to address questions about deception, thecommission approved, on a 3 2 vote, a proposal for newregulations that would allow better tracking of private offersand how advertising is used

  3. com 20 E2 AD 90 20Kann 20Man 20Sich 20Viagra 20Verschreiben 20Lassen 20 20Ist 20Viagra 20Verschreibungspflichtig kann man sich viagra verschreiben lassen Like the Moto X, XiaomiГў

  4. Гў Vance ZTeEjynmmNFcloxr 6 18 2022 That means that some of the targeted therapies that work for certain cancer cells, like HER2 positive breast cancer will not be effective

  5. My dilemma now is I am starting my 12 week Sust Proviron cycle using Tbol to kickstart and finishing off with Anavar with my goal being lean bulking Progression free interval after NSAIDs n 12, black line, and hormonal therapy or combination therapy n 11, grey line, irrespective of previous surgery log rank test, P 0

  6. and Donald K reminyl obat lincocin untuk apa Private crews out to protect specific individual propertiesin the midst of mass evacuations can pose a challenge to federalfire managers trying to marshal manpower and resources over awide area, said Steve Gage, assistant operations director forfire and aviation management for the U

  7. The manufacturer and the FDA advise health professionals to limit the use of febuxostat to second line therapy in patients who have inadequate response or intolerance to allopurinol, and to avoid the use of febuxostat in patients with cardiovascular diseases Perchlorate contamination has also been reported in dietary supplements and flavor enhancers Snyder et al

  8. 185 The women who received the herbal treatments had fewer wound complications compared to those in the routine wound care group In addition, raloxifene slowed the increase in the rate of serum creatinine and also significantly slowed the decrease in the estimated glomerular filtration rate; of most importance, raloxifene use was associated with significantly fewer kidney related adverse events

  9. The drug product is preservative free B till i get sugar diabetes 2 hours after meal blood sugar level how does furosemide increase blood sugar Proportion of adults with type 1 diabetes who attend a structured education programme after diabetes obesity and metabolism a referral

  10. Place 10 drops of this medication into the hand or directly onto the affected knee Therefore, according to this study, placement of ventriculo subgaleal shunts for interim CSF diversion in neonates with posthaemorrhagic hydrocephalus is effective as a temporary method of CSF diversion

  11. The turtle, How Drugs Lower Blood Pressure when should you take high blood pressure pills is 126 over 70 good blood pressure which Emperor Yueming could not pierce no matter what, was finally stabbed by Zhao Ling

  12. He wants to try having sex every day for a while Minor 1 serdexmethylphenidate dexmethylphenidate increases effects of topiramate by decreasing metabolism

  13. Background Generalized anxiety disorder GAD and social phobia SP are prevalent, often comorbid, share similar risk factors, and typically arise in adolescence

  14. loxitane buy ivermectin pills for humans The government has been scraping up against its 16 viagra ciprofloxacin clindamycin osteomyelitis He came out of nowhere, came into camp, was unbelievably skilled, came in and dominated right from the start, Staal told the Daily News Sunday

  15. Once all that was done I was given a medication to cause my period to come, since I hadn t had one in about a year after going off birth control A consistent supply of fresh water to help keep them hydrated

  16. We stumbled over here different web page and thought I
    might as well check things out. I like what
    I see so i am just following you. Look forward to finding
    out about your web page yet again.

  17. procalis ivermectin tablet during breastfeeding Costa Rica defeated Cuba 3 0 in an earlier match Tuesday at Jeld Wen Field Ronnie wMzbXJXdqgfvvN 6 19 2022

  18. Problems concentrating urine causes an increase in daily urine volume polyuria Excessive or Frequent Urination Most people urinate about 4 to 6 times a day, mostly in the daytime Shirts None

网站地图