免费建站 永久,怎样制作app文件,如何免费注册一个网站,wordpress用什么解析好总览
引导用户进入授权页面同意授权#xff0c;获取code通过code换取网页授权access_token#xff08;与基础支持中的access_token不同#xff09;如果需要#xff0c;开发者可以刷新网页授权access_token#xff0c;避免过期#xff08;一般不需要#xff09;通过网页…总览
引导用户进入授权页面同意授权获取code通过code换取网页授权access_token与基础支持中的access_token不同如果需要开发者可以刷新网页授权access_token避免过期一般不需要通过网页授权access_token和openid获取用户基本信息支持UnionID机制
获取code
解释让用户跳转到拼接后的指定地址进行授权获取code然后重定向到redirect_uri重定向后的地址带有code和state参数
拼接地址https://open.weixin.qq.com/connect/oauth2/authorize?appidappidredirect_uriredirect_uriresponse_typecodescopesnsapi_userinfostateSTATE#wechat_redirect其中appid和redirect_uri需要替换且redirect_uri需要encodeURI处理state为可选值用于用户自定义传参。
通过code换取access_token和用户信息 这里只写Java示例且使用weixin-java-mp依赖 引入以下依赖
dependencygroupIdcom.github.binarywang/groupIdartifactIdweixin-java-mp/artifactIdversion4.5.0/version
/dependency在application.yml中添加如下参数
# 自定义微信相关配置信息
wx:mp:# 微信公众号的appidappId: wxajhlksfnjls777777# 信公众号的app secretsecret: sds56dg4fd5sd54s5555555创建WxMpProperties如下
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;Component
Data
ConfigurationProperties(prefix wx.mp)
public class WxMpProperties {/*** 设置微信公众号的appid*/private String appId;/*** 设置微信公众号的app secret*/private String secret;
}创建WxMpConfiguration如下
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;Configuration
public class WxMpConfiguration {Autowiredprivate WxMpProperties wxMpProperties;/*** 微信客户端配置存储*/Beanpublic WxMpConfigStorage wxMpConfigStorage() {WxMpDefaultConfigImpl configStorage new WxMpDefaultConfigImpl();// 设置微信公众号appIdconfigStorage.setAppId(wxMpProperties.getAppId());// 设置微信公众号appSecretconfigStorage.setSecret(wxMpProperties.getSecret());return configStorage;}/*** WxMpService多个实现类 声明一个实例*/Beanpublic WxMpService wxMpService() {WxMpService wxMpService new WxMpServiceImpl();wxMpService.setWxMpConfigStorage(wxMpConfigStorage());return wxMpService;}
}然后在Controller中引入WxMpService
RestController
public class LoginController {Autowiredprivate WxMpService wxMpService;PostMapping(/wx/login)public SysAjaxResult login(RequestBody MapString, String params) {String code MapUtils.getString(params, code);if (StringUtils.isBlank(code)) {return SysAjaxResult.error(code不能为空);}try {// 获取accessToken获取用户信息据官方文档下面两个接口调用频率为每分钟5万次WxOAuth2AccessToken accessToken wxMpService.getOAuth2Service().getAccessToken(code);WxOAuth2UserInfo userInfo wxMpService.getOAuth2Service().getUserInfo(accessToken, null);// 通过openid校验有没有当前用户没有则新增String openid userInfo.getOpenid();return AjaxResult.success();} catch (WxErrorException e) {e.printStackTrace();}return AjaxResult.error();}
}时效
接口名频率限制通过 code 换取 access_token5 万/分钟获取用户基本信息5 万/分钟刷新 access_token10 万/分钟