微信获取openId--java

微信获取openId首先需要获取code,通过code代换出openId。现在我们通过微信测试号来获取openId:
配置好微信测试号后首先需要一个微信号关注测试号
在这里插入图片描述
直接通过微信号扫描测试号管理中的测试号二维码关注公众号即可。
公众号一般都是直接将微信链接嵌入公众号窗口中,所以我们首先先拼接微信链接
可以参考:https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html

https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect 

在这里插入图片描述
redirect_uri中可以定义一个参数来存放访问的URL,获取openId是可以将该路径解析出来进行跳转到任意路径

@Controller
@RequestMapping("********")
public class WxLoginController{
	
	/**
	 * 授权后重定向的回调链接地址
	 */
	@RequestMapping("*****")
	public String wxLogin(HttpServletRequest request, HttpServletResponse response) {
		String str = getUrl(request);
		System.out.println("str:"+str);
		try {
			//获取解析code
			String code=str.substring(str.indexOf("code=") + 5, str.indexOf("&st"));
			System.out.println("code:"+code);
			String openId = getOpenId(code);
			System.out.println("openId:"+openId);
			//解析授权后重定向的回调链接地址上面的附带参数 visitUrl:参数名
			//String visitUrl = request.getParameter("visitUrl");//根据需求添加
			//System.out.println("visitUrl:"+visitUrl);
			//return "redirect:"+visitUrl;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 获取openId
	 */
	public String getOpenId(String code) {
		String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=AppId&secret=AppSecret&code=CODE&grant_type=authorization_code";
		url = url.replace("AppId", "wxb855e55aad41adaf").replace("AppSecret", "80b202958d244b1c6e4ef242ad95da06").replace("CODE", code);
		System.out.println("url:"+url);
		String jsonResult = loadJSON(url);
		String ss = jsonResult.substring(jsonResult.indexOf("openid") + 9, jsonResult.indexOf("scope") - 3);
		return ss;
	}
	public static String getUrl(HttpServletRequest req) {
		String reqUrl = req.getRequestURL().toString();
		String queryString = req.getQueryString();
		if (queryString != null) {
			reqUrl += "?" + queryString;
		}
		return reqUrl;
	}
	public static String loadJSON(String url){
		StringBuilder json=new StringBuilder();
		BufferedReader in = null;
		try{
			URL oracle =new URL(url);
			URLConnection yc = oracle.openConnection();
			in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
			String inputLine =null;
			while ((inputLine=in.readLine())!=null){
				json.append(inputLine);
			}
		}catch(MalformedURLException e){
			System.out.println("HttpJSONTest ,MalformedURLException" + e.getMessage());
		}catch(IOException e){
			System.out.println("HttpJSONTest io" + e.getMessage());
		}
		finally{
			try {
				if(in != null){
					in.close();
				}
			} catch (Exception e2) {
				System.out.println("HttpJSONTest in.close()" + e2.getMessage());
			}
		}
		return json.toString();
	}
}
根据提供的引用内容,Java代码中的getopenid方法是用于获取微信公众号的openid的。该方法接收一个名为code的参数,通过调用微信API来获取openid。具体的步骤如下: 1. 首先,需要设置公众号的appid和secret。 2. 然后,设置响应头,允许跨域请求。 3. 构建微信登录的URL,包括appid、secret、code和授权类型等参数。 4. 发送GET请求到微信登录URL,获取返回的JSON字符串。 5. 解析JSON字符串,提取其中的openid字段作为结果。 6. 最后,将openid返回。 根据提供的代码,无法确定具体的错误原因。但是根据错误代码40029,这可能是由于code参数无效或过期导致的。建议检查传递给getopenid方法的code参数是否正确,并确保它是最新的。 请注意,以上回答是基于提供的引用内容,可能不完整或有遗漏。如果需要更详细的帮助,请提供更多相关的代码或信息。 #### 引用[.reference_title] - *1* *2* [微信公众号获取openidjava后端+html实现)](https://blog.csdn.net/weixin_43767744/article/details/121747536)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [微信公众号根据openid获取unionid](https://blog.csdn.net/qq_39418742/article/details/126197895)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值