程序员

微信获取openid

2016-05-13  本文已影响656人  鹏鹏袋

author:鹏鹏袋  day:2015.01.24

微信要获取openid

要准备appid,和回调地址,授权(明性授权snsapi_userinfo和隐性授权snsapi_base)

1:通过微信端打开网页获取一个code.

https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect

code 5分钟就会自动过期,只能使用一次

2:通过code获取asses_token和openid

https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code

他会回调一个json数据

里面有asscs_token和openid

Error 40029表示code无效

3:如果是明性授权,就要就要再调用一次来获取用户信息

通过access_token和openid来获取用户信息

https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN

调用成功的话,他会回一条json数据

Error 40003表示openid无效

附加功能:

让access_token持久化

在第二步拿到code之后,会有一个refresh_token

通过appid和refresh_token和access_token

https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=APPID&grant_type=refresh_token&refresh_token=REFRESH_TOKEN

调用成功的话他会返回一组json数据,把里面的assecc_token保存在数据库

检验access_token是否有效

通过openid和accsee_token

https://api.weixin.qq.com/sns/auth?access_token=ACCESS_TOKEN&openid=OPENID

返回结果是0表示成功

40003表示失败

微信获取OPENID优化流程

1:拿到$token =“gh_12312”; (微信gh号)

2:

://cdn�q~A|�d

上一篇 下一篇

猜你喜欢

热点阅读