Javaの+ JavaScriptのマイクロチャネルユーザ情報取得

最初のステップ:REDIRECT_URIは、インタフェースの背景である、あなたがそうでなければ、それは間違って行くだろう、ドメイン名tyg.comマイクロチャネルビジネスの終わりを設定する必要があります!このステップは、背景から直接開始することができます。

toLogin: function() {
	//拼接微信网页授权url
	var loginUrlx = "https://open.weixin.qq.com/connect/oauth2/authorize?";
	var appIdParamx = "appid=wx35bd97132456&";
	var redirectParamx = "redirect_uri=http://tyg.com/api/getCode&";
	var scopeParamx = "scope=snsapi_userinfo&";
	var responseParamx = "response_type=code&";
	var stateParamx = "state=http://h5.yjj.com/component/index.html";
	var endingParamx = "#wechat_redirect";
	//最终拼接后的授权url
	var lastUrlx = loginUrlx + appIdParamx + redirectParamx + scopeParamx + responseParamx + stateParamx + endingParamx;
	//跳转到微信网页授权页
	window.location.href = lastUrlx;
},

ステップ2:マイクロチャンネルのユーザーを取得するための背景情報要求、これが唯一のOpenIDなど、access_tokenはを取得することができますが、開発者モードの開口部ならば、あなたもunionid得ることができます。

/**
 * 获得code后微信授权回调,重定向到指定页面
 * @param req
 * @param rsp
 * @param code		微信的code
 * @param state		请求时传入参数,微信回调会一起返回
 * @throws Exception
 * @return void
 * @author tyg
 * @date   2019年3月6日下午1:34:04
 */
@RequestMapping(value = "/api/wechat/auth", method = RequestMethod.GET)
public JSONObject auth(HttpServletRequest req, HttpServletResponse rsp, String code) throws Exception {
	Map<String, String> params = new HashMap<String, String>();
	params.put("grant_type", "authorization_code");//
	params.put("appid", "wx1245f8ds478");
	params.put("secret", "15646fs8f9d7f4fa8f64s6f4");
	params.put("code", code);

	String jstoken = HttpUtils.sendGet("https://api.weixin.qq.com/sns/oauth2/access_token", params);

	if (jstoken == null) {
		logger.error("jstoken is null");
		return null;
	}
	JSONObject jsonResult = JSONObject.parseObject(jstoken);
	logger.info("getAccess_token:" + jstoken);
	if (null != jsonResult.get("errcode")) {
		Assert.throwException("wechat web auth err:" + jsonResult.get("errcode") + ":" + jsonResult.get("errmsg"));
	} else {
		return jsonResult;
	}
}

3ステップ:あなたは、ユーザーのアバターを取得する必要がある場合は、情報をニックネームでなく、呼び出します。

/**
 * 获取微信用户的昵称、头像
 * @param obj
 * @return
 * @throws Exception
 * @return JSONObject
 * @author tyg
 * @date   2019年5月8日下午7:06:12
 */
public JSONObject getWechatUserInfo(JSONObject obj) throws Exception {
	String access_token = obj.getString("access_token");
	String openid = obj.getString("openid");
	Map<String, String> params = new HashMap<String, String>();
	params.put("access_token", access_token);
	params.put("openid", openid);
	params.put("lang", "zh_CN");
	String userinfo = HttpUtils.sendGet("https://api.weixin.qq.com/sns/userinfo", params);
System.out.println(userinfo);
	JSONObject userJson = JSONObject.parseObject(userinfo);
	if (null != userJson.get("errcode")) {
		Assert.throwException("get wechat user error:" + userJson.get("errcode") + ":" + userJson.get("errmsg"));
	} else {
		return userJson;
	}
	return userJson;
}

 

おすすめ

転載: blog.csdn.net/qq_26365837/article/details/89967390