如何获取微信用户openid

如何获取微信用户openid

com.github.binarywang

weixin-java-mp

3.1.0

3.2、将微信公众号配置写入yaml文件并引入类中

wechat:

mpAppId: 你的微信测试号appId

mpAppSecret: 你的微信测试号secret

import lombok.Data;

import org.springframework.boot.context.properties.ConfigurationProperties;

import org.springframework.stereotype.Component;

/**

* @author :小肖

* @date :Created in 2022/2/2 10:31

*/

@Component

@Data

@ConfigurationProperties(prefix = "wechat")

public class WechatAccountConfig {

/**

* 公众号id

*/

private String mpAppId;

/**

* 公众号密钥

*/

private String mpAppSecret;

}

3.3、编写配置类初始化设置wxMpService配置

import me.chanjar.weixin.mp.api.WxMpConfigStorage;

import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;

import me.chanjar.weixin.mp.api.WxMpService;

import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.context.annotation.Bean;

import org.springframework.stereotype.Component;

/**

* @author :小肖

* @date :Created in 2022/2/2 10:24

*/

@Component

public class WechatMpConfig {

@Autowired

private WechatAccountConfig wechatAccountConfig;

@Autowired

private WxMpInMemoryConfigStorage wxMpInMemoryConfigStorage;

@Bean

public WxMpService wxMpService(){

WxMpService wxMpService = new WxMpServiceImpl();

wxMpService.setWxMpConfigStorage(wxMpInMemoryConfigStorage);

return wxMpService;

}

@Bean

public WxMpInMemoryConfigStorage wxMpConfigStorage(){

/**

* 这里需要注意的是 由于父类中没有定义对应的接口

* 所以所有的方法都在其实现类中,所以我们要构造实现类

*/

WxMpInMemoryConfigStorage wxMpConfigStorage = new WxMpInMemoryConfigStorage();

wxMpConfigStorage.setAppId(wechatAccountConfig.getMpAppId());

wxMpConfigStorage.setSecret(wechatAccountConfig.getMpAppSecret());

return wxMpConfigStorage;

}

}

3.4、编写对应的controller

import com.xiao.enums.ResultEnum;

import com.xiao.exception.SellException;

import lombok.extern.slf4j.Slf4j;

import me.chanjar.weixin.common.api.WxConsts;

import me.chanjar.weixin.common.error.WxErrorException;

import me.chanjar.weixin.mp.api.WxMpService;

import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.bind.annotation.RestController;

/**

* @author :小肖

* @date :Created in 2022/2/2 10:20

*/

@Controller

@RequestMapping("/wechat")

@Slf4j

public class WechatController {

@Autowired

private WxMpService wxMpService;

@GetMapping("/authorize")

public String authorize(@RequestParam("returnUrl") String returnUrl){

String url = "http://xiao-sell.natapp1.cc/sell/wechat/userInfo";

String redirectUrl = wxMpService.oauth2buildAuthorizationUrl(url, WxConsts.OAuth2Scope.SNSAPI_USERINFO,returnUrl);

return "redirect:" + redirectUrl;

}

@GetMapping("/userInfo")

public String userInfo(@RequestParam("code") String code,

@RequestParam("state") String returnUrl) {

WxMpOAuth2AccessToken wxMpOAuth2AccessToken = new WxMpOAuth2AccessToken();

try{

wxMpOAuth2AccessToken = wxMpService.oauth2getAccessToken(code);

}catch (WxErrorException e){

log.error("【微信网页授权错误】 exception = {}",e);

throw new SellException(ResultEnum.WECHAT_MP_ERROR.getCode(),e.getError().getErrorMsg());

}

String openId = wxMpOAuth2AccessToken.getOpenId();

log.info("openid = {}",openId);

return "redirect:" + returnUrl + "?openid=" + openId;

}

}

3.5、进行debug测试

第一个断点

该重定向的url很明显就是我们手工方式中获取code的url。

第二个断点

成功获取了code和openid。

相关推荐

医砭 » 症状鑑别 » 皮肤皲裂
365bet体育足球

医砭 » 症状鑑别 » 皮肤皲裂

📅 07-20 👁️ 5608
题解 - 步步夺金
bt365官方网注册

题解 - 步步夺金

📅 09-26 👁️ 2129
编织中国结的方法过程(编织中国结的制作过程)
365bet体育足球

编织中国结的方法过程(编织中国结的制作过程)

📅 07-14 👁️ 4194
上滤鱼缸的最佳过滤方法,分享 4 种不错的设计!
365bet官网体育娱乐

上滤鱼缸的最佳过滤方法,分享 4 种不错的设计!

📅 10-02 👁️ 6727
超鱼手机版下载
365bet体育足球

超鱼手机版下载

📅 09-08 👁️ 1401
FIFA Online3 WB卡全球员评测 WB卡球员推荐
bt365官方网注册

FIFA Online3 WB卡全球员评测 WB卡球员推荐

📅 07-15 👁️ 8517