场景:微信公众平台-微信网页授权
将服务上传到服务器(该服务器有域名)
域名为例如我的服务器为 https://damionew.top
代码如下:
package com.test;
import java.io.IOException;
import java.net.URLEncoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* 处理微信请求
* @author yinyunqi
*
*/
@Controller
public class WeChatController {
@Autowired
WeChatService weChatService;
// 测试号平台 https://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index
// 公众号的唯一标识【必需】
public static final String APPID = "appid";
// 授权后重定向的回调链接地址, 请使用 urlEncode 对链接进行处理【必需】
public static final String REDIRECT_URI = "https://damionew.top/wx/login";
// 返回类型,请填写code【必需】
public static final String RESPONSE_TYPE = "code";
// 应用授权作用域【必需】
// snsapi_base (不弹出授权页面,直接跳转,只能获取用户openid)
// snsapi_userinfo (弹出授权页面,可通过openid拿到昵称、性别、所在地。并且, 即使在未关注的情况下,只要用户授权,也能获取其信息 )
public static final String SCOPE = "snsapi_userinfo";
// 重定向后会带上state参数,开发者可以填写a-zA-Z0-9的参数值,最多128字节【非必需】
public static final String STATE = "123";
// 无论直接打开还是做页面302重定向时候,必须带此参数【必需】
public static final String wechat_redirect = "";
@RequestMapping("/weChat")
public String weChatTest() {
return "weChat";
}
@ResponseBody
@RequestMapping("/weChat/auth")
public void weChatAuth(HttpServletRequest req,HttpServletResponse res) {
String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+APPID
+ "&redirect_uri="+REDIRECT_URI
+ "&response_type="+RESPONSE_TYPE
+ "&scope="+SCOPE
+ "&state="+STATE
+ "#wechat_redirect";
try {
res.sendRedirect(url);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
报出微信网页授权错误码10003:redirect_uri域名与后台配置不一致错误的几种可能:
1.REDIRECT_URI需要使用域名下的url,即必须是所请求域名下的网址
2.公众平台测试管理中 网页授权获取用户基本信息 —> OAuth2.0网页授权不带https,只填写域名
这两处做了反复测试,是报出10003的主要原因