微信公众号OAuth 点击菜单按钮 跳转页面授权 获取openid
流程:
用户点击菜单按钮
—–> 发送页面授权请求到微信后台
—–> 得到 code 发送到 我们服务器后台的url
—–> 将 code 参数发送到微信后台获取用户的openid返回服务器上的页面(也可以直接获取用户头像等基本信息,需用户点击同意授权)
菜单 view 按钮的 url:
“https://open.weixin.qq.com/connect/oauth2/authorize?appid=“+yourAppid+”&redirect_uri=”+你要跳转的url+”&response_type=code&scope=snsapi_base&state=1#wechat_redirect”
详情请见官方文档
http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html
要跳转的 url 请求到后台 获取页面授权的 code:
String code = request.getParameter("code"); // 页面授权获取的code用以交换 openid 的 access_token
openid = OAuthGetOpenid(code); //OAuth获取用户openid区分是哪个用户请求登录W3
/**
* 通过页面OAuth授权,用code获取openid
*
* @param code
* @return
*/
public String OAuthGetOpenid(String code) {
JSONObject json = new JSONObject();
String requestUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=你的公众号id&secret=你的公众号密码&code=" + code + "&grant_type=authorization_code";
String outputStr = "";
new HttpRequestPost();
json = HttpRequestPost.httpRequest(requestUrl, outputStr, "POST");
String openid = json.getString("openid");
return openid;
}
发送http请求的方法代码:
/***
* 向微信端发送post请求
*
*/
public class HttpRequestPost {
public static JSONObject httpRequest(String requestUrl, String outputStr, String method) {
JSONObject jsonObject = null;
StringBuffer buffer = new StringBuffer();
try {
// 创建SSLContext对象,并使用我们指定的信任管理器初始化
TrustManager[] tm = { new MyX509TrustManager() };
SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
sslContext.init(null, tm, new java.security.SecureRandom());
// 从上述SSLContext对象中得到SSLSocketFactory对象
SSLSocketFactory ssf = sslContext.getSocketFactory();
URL url = new URL(requestUrl);
HttpsURLConnection httpUrlConn = (HttpsURLConnection) url.openConnection();
httpUrlConn.setSSLSocketFactory(ssf);
httpUrlConn.setDoOutput(true);
httpUrlConn.setDoInput(true);
httpUrlConn.setUseCaches(false);
// 设置请求方式(GET/POST)
httpUrlConn.setRequestMethod(method);
httpUrlConn.connect();
// 当有数据需要提交时
if (null != outputStr) {
OutputStream outputStream = httpUrlConn.getOutputStream();
// 注意编码格式,防止中文乱码
outputStream.write(outputStr.getBytes("UTF-8"));
outputStream.close();
}
// 将返回的输入流转换成字符串
InputStream inputStream = httpUrlConn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
bufferedReader.close();
inputStreamReader.close();
// 释放资源
inputStream.close();
inputStream = null;
httpUrlConn.disconnect();
jsonObject = JSONObject.fromObject(buffer.toString());
System.out.println("http-back:" + jsonObject);
} catch (ConnectException ce) {
System.err.println("Weixin server connection timed out.");
} catch (Exception e) {
System.err.println("https request error:{}" + e);
}
return jsonObject;
}
}
以上转自:http://blog.csdn.net/sinat_15114467/article/details/50635220?locationNum=10&fps=1
其实很简单,就是一个微信的url 的访问接口,通过请求连接,直接get返回的结果中对应的属性值就可以了!所有获取的什么ticket票据、access_token等等之类的都是这个思想,也就是所谓的面向服务编程(SOA,Service Oriented Archtiecuture)!