【Way to Java】微信公众号网页授权

微信公众号网页授权

需求公众号上通过网页授权,获取用户基本信息登陆

前置工作

  1. 一个服务号(订阅号无法调用网页授权)
  2. 确认网页授权权限已打开
  3. 确认服务器域名已配置
    在这里插入图片描述
  4. 确认appId和appSecret已配置
    在这里插入图片描述

微信官方文档说明

第一步:引导用户进入授权页面同意授权,获取code

页面请求如下地址:
https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect
appid:必填。公众号的唯一标识
redirect_uri:必填。授权后重定向的回调链接地址,使用 urlEncode 对链接进行处理
response_type:必填。返回类型,填写code
state:选填。重定向后会带上state参数,开发者可以填写a-zA-Z0-9的参数值,最多128字节
#wechat_redirect:必填。无论直接打开还是做页面302重定向时候,必须带此参数
scope:必填 二选一。如果填snsapi_base,静默授权,不弹出授权页面,直接跳转,只能获取用户openid如果填snsapi_userinfo,主动授权,弹出授权页面,可通过openid拿到昵称、性别、所在地。并且, 即使在未关注的情况下,只要用户授权,也能获取其信息

第一步的目的是拿到code和state,code作为换取access_token的票据,每次用户授权带上的code将不一样,code只能使用一次,5分钟未被使用自动过期

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

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

获取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

第二步的目的是拿到特殊的网页授权access_token。如果网页授权的作用域为snsapi_base,则本步骤中获取到网页授权access_token的同时,也获取到了openid,snsapi_base式的网页授权流程即到此为止如果网页授权的作用域为snsapi_userinfo,则可以继续通过openId和access_token拉去用户信息

由于appSecret和获取到的access_token安全级别都非常高,必须只保存在服务器,不允许传给客户端
刷新access_token、通过access_token获取用户信息等步骤,也必须从服务器发起

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

access_token拥有较短的有效期(7200秒),当access_token超时后,可以使用refresh_token进行刷新,refresh_token有效期为30天,当refresh_token失效之后,需要用户重新授权。
https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=APPID&grant_type=refresh_token&refresh_token=REFRESH_TOKEN
appid:必填。公众号的唯一标识
grant_type:必填。固定填写为refresh_token
refresh_token:必填。填写通过access_token获取到的refresh_token参数

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

如果网页授权作用域为snsapi_userinfo,则此时开发者可以通过access_token和openid拉取用户信息
https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN
access_token:必填。网页授权接口调用凭证,access_token与基础支持的access_token不同
openid:必填。用户的唯一标识
lang:必填。返回国家地区语言版本,zh_CN 简体,zh_TW 繁体,en 英语

业务逻辑

在这里插入图片描述

主要代码

在这里插入图片描述
在这里插入图片描述
userService.queryWechatAuthInfo对应第二步的get请求;
userService.queryWechatUserInfo对应第四部的get请求;

备注

  1. 小程序可以获取用户手机号信息,公众号好像是不可以
  2. 本文不当之处还请留言指正
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Yes, there is another way to use CToolTipCtrl in an MFC application. Instead of adding the tooltip for a control in the OnInitDialog function, you can add the tooltip for a control in response to a user action, such as clicking a button. Here are the steps to use CToolTipCtrl in this way: 1. Create a CToolTipCtrl object: ```CToolTipCtrl m_ToolTip;``` 2. Initialize the CToolTipCtrl object in the OnInitDialog function of your dialog: ``` BOOL CMyDialog::OnInitDialog() { CDialogEx::OnInitDialog(); // Create the tooltip control m_ToolTip.Create(this); return TRUE; } ``` 3. Add a message map entry for the control that you want to associate with the tooltip: ``` BEGIN_MESSAGE_MAP(CMyDialog, CDialogEx) ON_CONTROL_RANGE(CBN_SELCHANGE, IDC_MYCONTROL1, IDC_MYCONTROL2, &CMyDialog::OnMyControlSelChange) END_MESSAGE_MAP() ``` This message map entry handles the CBN_SELCHANGE message for a range of controls, from IDC_MYCONTROL1 to IDC_MYCONTROL2. 4. Add a handler for the message map entry that adds the tooltip for the control: ``` void CMyDialog::OnMyControlSelChange(UINT nID) { CString strTooltip; // Determine the tooltip text based on the control ID switch (nID) { case IDC_MYCONTROL1: strTooltip = _T("My Tooltip 1"); break; case IDC_MYCONTROL2: strTooltip = _T("My Tooltip 2"); break; } // Add the tooltip for the control m_ToolTip.AddTool(GetDlgItem(nID), strTooltip); } ``` This handler determines the tooltip text based on the control ID and adds the tooltip for the control using the AddTool method. 5. Handle the WM_MOUSEMOVE message in your dialog to display the tooltip: ``` BOOL CMyDialog::PreTranslateMessage(MSG* pMsg) { // Pass mouse messages to the tooltip control m_ToolTip.RelayEvent(pMsg); return CDialogEx::PreTranslateMessage(pMsg); } ``` This method handles the WM_MOUSEMOVE message and passes it to the tooltip control using the RelayEvent method. That's it! Now when the user selects a control, the tooltip will be displayed. You can also remove the tooltip for a control using the DeleteTool method.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值