sdk问题
来源:7-6 使用sdk方式(下)
tidhy
2018-02-16
在做网页授权url的时候,师兄的代码是这样子写的:
@GetMapping("/authorize")
public String authorize(@RequestParam("returnUrl") String returnUrl) {
//1. 配置
//2. 调用方法
//url:http://sell.natapp4.cc/sell/wechat/userInfo
String url = projectUrlConfig.getWechatMpAuthorize() + "/sell/wechat/userInfo";
String redirectUrl = wxMpService.oauth2buildAuthorizationUrl(url, WxConsts.OAUTH2_SCOPE_BASE, URLEncoder.encode(returnUrl));
return "redirect:" + redirectUrl;
}但是我进入
wxMpService.oauth2buildAuthorizationUrl
的实现方法中看到下面的代码:
public String oauth2buildAuthorizationUrl(String redirectURI, String scope, String state) {
return String.format("https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=%s&state=%s#wechat_redirect", this.getWxMpConfigStorage().getAppId(), URIUtil.encodeURIComponent(redirectURI), scope, StringUtils.trimToEmpty(state));
}这个实现的方法最后不是把这段地址都return回去了么?我感觉redirectUrl最后的值应该是:
https://open.weixin.qq.com/connect/qrconnect?appid=wxd898fcb01713c658&redirect_uri=www.imooc.com&response_type=code&scope=snsapi_base&state=#wechat_redirect
但是仔细想想这个肯定是错的,因为这样子的话就无法调用接收code的方法了,很烦呀,是我sdk没有读懂么?
写回答
1回答
-
wxMpService.oauth2buildAuthorizationUrl 方法的redirectURI参数,传进来的是
String url = projectUrlConfig.getWechatMpAuthorize() + "/sell/wechat/userInfo";
这行代码构造出来的url,跳转也是跳转的这个url,但是在state参数中携带了我们最终要跳转的url(也就是authorize的参数)
所以这个授权一共经历了三次跳转
第一次:跳转到微信授权界面
第二次:用户授权以后跳转到了项目的/sell/wechat/userInfo 路径(String url = projectUrlConfig.getWechatMpAuthorize() + "/sell/wechat/userInfo";)
第三次:进入/sell/wechat/userInfo 后,从url的参数中获取了code 和state参数的值,根据code拿到token进一步得到openId后,又重定向到了state参数的url,这个state参数,追根溯源,就是我们最开始authorize
方法传递的参数052018-02-19
相似问题