一、普通方法获取
1.获取openid需要微信认证的服务号及以上权限才可以,如果是个人学习可以申请一个测试账号。
申请测试账号:/debug/cgi-bin/sandbox?t=sandbox/login
2.微信号关注测试账号。
3.修改回调页面域名(如果没有域名,可以使用花生壳等内网穿透工具)
4.进入消息接口使用指南,进入微信网页开发,进入微信网页授权
5.根据相应提示,拼接成一个完整的url,在微信端打开。
url示例:
/connect/oauth2/authorize?appid=***&redirect_uri=http://186***/sell/weixin/auth&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect
测试是否能获取对应的信息
@Slf4j @RestController @RequestMapping("weixin") public class WeixinController { @GetMapping("auth") public void auth(@RequestParam("code") String code) { log.info("进入验证"); log.info("code={}", code); String url = "/sns/oauth2/access_token?appid=wx111f1b7437e708dd&secret=e12860f0ce3d177f39e2160a0c8286c1&code=" + code + "&grant_type=authorization_code"; RestTemplate restTemplate = new RestTemplate(); String response=(url,String.class); log.info("response={}",response); }
7.微信端打开链接,测试通过!
2018-01-22 14:21:39,721 - 进入验证
2018-01-22 14:21:39,722 - code= 0211m1EK15XJC60PZUHK1Lw0EK11m1EM
2018-01-22 14:21:39,864 - response={"access_token":"6_PI28kdwbshmOV7pjOEt-7d00n5RadMrK8mQPjPmnN5wIc-Tq_lC3uN80ciF8FeW95bUqHINZnyCWtvEXfwbgyV0_PxP7rLv71qw-2khXS88","expires_in":7200,"refresh_token":"6_mAl5UpV30kKlwsDt5ni68ZQrErwoHlFA57WTXtx554yURUyCY6mB9xDw88Fi-vyYt-yCBgc-Rkr-VamKlKQvjpp6yUxqApUC1Fzk69PiD3Y","openid":" oakir0f-2BQXKjR7ARMHt1iwTQdE","scope":"snsapi_userinfo"}
2018-01-22 14:21:39,722 - code= 0211m1EK15XJC60PZUHK1Lw0EK11m1EM
2018-01-22 14:21:39,864 - response={"access_token":"6_PI28kdwbshmOV7pjOEt-7d00n5RadMrK8mQPjPmnN5wIc-Tq_lC3uN80ciF8FeW95bUqHINZnyCWtvEXfwbgyV0_PxP7rLv71qw-2khXS88","expires_in":7200,"refresh_token":"6_mAl5UpV30kKlwsDt5ni68ZQrErwoHlFA57WTXtx554yURUyCY6mB9xDw88Fi-vyYt-yCBgc-Rkr-VamKlKQvjpp6yUxqApUC1Fzk69PiD3Y","openid":" oakir0f-2BQXKjR7ARMHt1iwTQdE","scope":"snsapi_userinfo"}
ps:
如上所示,用户通过url访问微信,微信通过url上提供的相应的信息,重定向到我们指定的地址(含有code),在controller中可以获取code,同时controller根据这个code,和其他的信息,访问微信,可以获取到一系列信息,包括最重要的openid。
二、根据第三方工作包,便捷获取。
地址:/Wechat-Group/weixin-java-tools
2.
MP_OAuth2网页授权文档:/Wechat-Group/weixin-java-tools/wiki/MP_OAuth2%E7%BD%91%E9%A1%B5%E6%8E%88%E6%9D%83
依赖
<dependency>
<groupId></groupId>
<artifactId>weixin-java-mp</artifactId>
<version>2.9.0</version>
</dependency>
5.进行优化:(可以略过)
application配置
wechat: appId: wx***dd appSecret: e1286***86c1
@Component @ConfigurationProperties(prefix = "wechat") public class WeChatAccountConfig { //省略set get,(导入application配置) private String appId; private String appSecret; }
@Component public class WeChatMpConfig { @Autowired private WeChatAccountConfig accountConfig; @Bean public WxMpService wxMpService(){ WxMpService wxMpService=new WxMpServiceImpl(); (wxMpConfigStorage()); return wxMpService; } @Bean public WxMpConfigStorage wxMpConfigStorage(){ WxMpInMemoryConfigStorage wxMpInMemoryConfigStorage=new WxMpInMemoryConfigStorage(); (accountConfig.getAppId()); (accountConfig.getAppSecret()); return wxMpInMemoryConfigStorage; } }
@Slf4j @RequestMapping("wechat") @Controller public class WeChatController { @Autowired private WxMpService wxMpService; @GetMapping("authorize") public String authorize(){ //1.配置 //2.调用方法 String url="http://186***/sell/wechat/userInfo"; String redirectUrl=wxMpService.oauth2buildAuthorizationUrl(url, WxConsts.OAuth2Scope.SNSAPI_BASE,null); log.info("【微信网页授权】获取code,result={}",redirectUrl); return "redirect:"+redirectUrl; } @GetMapping("userInfo") public void userInfo(@RequestParam("code")String code){ WxMpOAuth2AccessToken wxMpOAuth2AccessToken; try { wxMpOAuth2AccessToken = wxMpService.oauth2getAccessToken(code); }catch (WxErrorException e){ log.error("【微信网页授权】{}",e); throw new SellException(ErrorEnum.WX_MP_ERROR.getCode(),().getErrorMsg()); } String openid=(); log.info("openid={}",openid); } }