微信域名回調(diào)
標(biāo)簽(空格分隔): springboot java wechat
普通方法
- 設(shè)置域名(內(nèi)網(wǎng)穿透工具natapp)
需要在微信中設(shè)置回調(diào)域名
- 獲取code
經(jīng)過(guò)發(fā)送 微信服務(wù)器,微信重定向到本地蔚约。
https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect
APPID = 對(duì)接公眾號(hào)賬戶id
REDIRECT_URI = 回調(diào)域名地址
SCOPE = 獲取用戶信息方式兩種(snsapi_base溪厘、snsapi_userinfo)
- 換取access_token
通過(guò)重定向獲取code,重新發(fā)送請(qǐng)求獲取用戶信息钞艇。
https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
APPID = 對(duì)接公眾號(hào)賬戶id
SECRET = 對(duì)接公眾號(hào)賬戶secret
CODE = 重定向獲取的id
/**
* 獲取code測(cè)試
* @param code
*/
@GetMapping("/auth")
public void auth(@RequestParam("code") String code){
log.info("code={}",code);
String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code="+code+"&grant_type=authorization_code";
RestTemplate restTemplate = new RestTemplate();
String response = restTemplate.getForObject(url,String.class);
log.info("response={}",response);
}
使用SDK方法
- 引用
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
<version>2.8.0</version>
</dependency>
- 獲取code
//引用配置參數(shù)
@Data
@Component
@ConfigurationProperties(prefix = "wechat")
public class WechatAccountConfig {
/** 賬戶 .*/
private String wechat_appid;
/** 密碼 .*/
private String wechat_secret;
}
//參數(shù)設(shè)置
@Component
public class WechatMpConfig {
@Autowired
private WechatAccountConfig wechatAccountConfig;
@Bean
public WxMpService wxMpService(){
WxMpService wxMpService = new WxMpServiceImpl();
wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
return wxMpService;
}
@Bean
public WxMpConfigStorage wxMpConfigStorage(){
WxMpInMemoryConfigStorage wxMpConfigStorage = new WxMpInMemoryConfigStorage();
wxMpConfigStorage.setAppId(wechatAccountConfig.getWechat_appid());
wxMpConfigStorage.setSecret(wechatAccountConfig.getWechat_secret());
return wxMpConfigStorage;
}
}
//獲取code
/**
* 獲取code
* @param returnUrl
* @return
*/
@GetMapping("/authrize")
public String authrize(@RequestParam("returnUrl") String returnUrl){
String url = "http://www.xiaojinzi.top/sell/wechat/userInfo";
String rediectUrl = wxMpService.oauth2buildAuthorizationUrl(url,WxConsts.OAUTH2_SCOPE_USER_INFO, URLEncoder.encode(returnUrl));
return "rediectUrl:" + rediectUrl;
}
- 獲取openid
/**
* 微信openid
* @param code
* @param returnUrl
* @return
*/
@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("【微信網(wǎng)頁(yè)授權(quán)】 e={}",e);
throw new SellException(ResultEnum.WECHAT_WEB_ERROR.getCode(),e.getError().getErrorMsg());
}
String openid = wxMpOAuth2AccessToken.getOpenId();
return "rediectUrl:"+ returnUrl + "?openid="+openid;
}
前端調(diào)試
- 設(shè)置前端參數(shù)
cd /opt/code/sell_fe_buyer/config/
vim index.js
npm run build
- 抓包使用fiddle 或者 anyproxy 操作請(qǐng)查看githubwiki
- 原視頻UP主慕課網(wǎng)(SpringBoot企業(yè)級(jí)微信點(diǎn)餐項(xiàng)目)
- 本篇博客撰寫(xiě)人: XiaoJinZi 轉(zhuǎn)載請(qǐng)注明出處
- 學(xué)生能力有限 附上郵箱: 986209501@qq.com 不足以及誤處請(qǐng)大佬指責(zé)