你好,
我在这里遇到了可能和别人相似的错误。我代码应该跟目前视频中的应该一样。
但还是不能重定向到/userInfo页面
public class WechatController {
@Autowired
private WxMpService wxMpService;
@GetMapping("/authorize")
public String authorize(@RequestParam("returnUrl") String returnUrl) {
//1.配置
//2.调用方法
String url = "http://www.sellshanjun.s1.natapp.cc/sell/wechat/userInfo";
String redirectUrl = wxMpService.oauth2buildAuthorizationUrl(url, WxConsts.OAuth2Scope.SNSAPI_USERINFO, returnUrl);
log.info("【微信网页授权】获取code, result={}",redirectUrl);
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("微信网页授权{}",e);
throw new SellException(ResultEnum.WX_MP_ERROR.getCode(),e.getError().getErrorMsg());
}
String openid = wxMpOAuth2AccessToken.getOpenId();
log.info("微信网页授权,获得openid{}",openid);
return "redirect:"+returnUrl+"?openid="+openid;
}
}
打开连接时用的URL:
http://sellsxxxxxxxxxcc/sell/wechat/authorize?returnUrl=http:%2F%2Fwww.imooc.com
log打印时的信息【微信网页授权】获取code, result=https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx-myappid&redirect_uri=http%3A%2F%2Fwww.sellXXXXXXXXX.cc%2Fsell%2Fwechat%2FuserInfo&response_type=code&scope=snsapi_userinfo&state=http://www.imooc.com#wechat_redirect
是否是跟微信的白名单有关系?