微信网页授权 的流程

下面这个链接为微信发起 OAuth 的跳转地址

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

注意链接中有几个变量需要替换

  • APPID 测试账号中的 appID,填写自己账号的 appID
  • REDIRECT_URI 用户同意授权后的回调地址,填写 http://larabbs.test
  • SCOPE 应用授权作用域,填写 snsapi_userinfo
  • STATE 随机参数,可以不填,我们保持 STATE 即可。

替换后我们得到的链接类似

https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxe0ba316xxxxxxx&redirect_uri=http://larabbs.test&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect

在开发者工具中,访问该链接,可以看到微信授权页面

点击确认登录

我们成功的跳转回了 REDIRECT_URI,注意 url 中可以看到 code 参数。好了我们已经完成了 OAuth 流程中获取授权码的步骤。

请求以下链接获取 access_token:

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

需要替换的变量

  • APPID 测试账号中的 appID,填写自己账号的 appID
  • SECRET 测试账号中的 secret,填写自己账号的 secret
  • code 上一步获取的 code

替换后的链接如下

https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx353901f6xxxxx&secret=d4624c36b6795d1d99dxxxxxxxx&code=0813AoG21n9C2O1yfxH21t4nG213AoGH&grant_type=authorization_code

使用 PostMan 访问该链接,获取到了 access_token,注意微信同时返回了 open_id,微信 access_token 和 open_id 一起请求用户信息。

通过 access_token 获取个人信息

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

替换链接中的 ACCESS_TOKEN 和 OPENID,使用 PostMan 访问

原文地址:https://www.cnblogs.com/sgm4231/p/11097107.html