微信公众平台测试号,页面获取openid

网页授权获取用户基本信息
http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html

一、获取网页授权

scope=snsapi_base,只获取openid,静默获取,不会有提示

原有网页链接需改为先访问oAuth获得授权后再跳转到原链接,

原链接:http://111.222.333.444/app/test.action

修改后:

https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxf21ea8f5294dce18&redirect_uri=http://111.222.333.444/app/test.action&response_type=code&scope=snsapi_base&state=123#wechat_redirect

二、通过code换取openid

扫描二维码关注公众号,回复: 710094 查看本文章

页面获取code:request.getParameter("code");

//通过code获取openId
public static String getOpenId(String code){
	if(StringUtil.isNotEmpty(code)){
		String appid = "wxf21ea8f5294dce18";
		String secret = "d4624c36b6795d1d99dcf0547af5444e";
		String result = HttpUtil.doHttpsGet("https://api.weixin.qq.com/sns/oauth2/access_token?appid="+appid+"&secret="+secret+"&code="+code+"&grant_type=authorization_code", null, "UTF-8", true);
		if(StringUtil.isNotEmpty(result)){
			JSONObject json = JSONObject.fromObject(result);
			if(json.get("openid")!=null){
				return json.get("openid").toString();
			}
		}
	}
	return "";
}

HttpUtil

Java用HttpClient3发送Https的Get和Post请求(增强)

http://happyqing.iteye.com/blog/2266742

scope参数错误或没有scope权限
开发者需要先到公众平台官网中的开发者中心页配置授权回调域名(测试号管理里也可以)
网页帐号  网页授权获取用户基本信息  无上限 修改

OAuth2.0网页授权

配置域名或ip

 


redirect_uri参数错误
配置的域名或ip,

以前后面不能写端口号,http(80),https(443)的都可以

现在写端口号也可以了,111.222.333.444:8020

猜你喜欢

转载自happyqing.iteye.com/blog/2266764