告知:所有操作均基于域名已备案,3358www.Sina.com/!
1 .准备工作
1.1 .注册微信开放平台账户
https://open.weixin.qq.com
1.2 .建立APP (网站APP )并填写资料
3359 open.weixin.QQ.com/CGI-bin/app create? t=manage/createwebtype=applang=zh _ cn token=f 95883 EFA2cf 7187 c 7b 020d3CD 26 a 53e 89 B4 DC 913http://www.Sina.com /
企业级微信公众号
注意:
!填写的域名与备案号必须一致.
!!授权回调域名必须与微信公共号的授权域名一致:权限接口(修改)----设置----填写域名! 微信开放平台:
微信公共平台
!需要将xxx.txt文件上传到服务器根目录
1.3 .申请成为开发者(不申请,无权调用微信扫描码登录界面) ) ) )。
1.3.1 .开通申请
1.3.2 .填写资料
1.3.3 .拿生命来!……不,付钱,300大洋
2 .微信扫描码注册流程图(自己画的…) )。
你可以去看官网。 这里有自己的理解,还有问题。 此二维码委托我们的服务器作为用户,然后我们的服务器会向他返回二维码,但对开发者来说则不然。 这里请不要给我错拿走。 浏览官网就知道了。
3 .实现
首先,前端一定有后台的请求。 此请求用于获取微信登录二维码。
3.1.1 .前端要求
例如, a href='/wexinlogin/XXX '微信登录/a,当然后台总是被映射。
3.2.1 .组装URL,向微信发送请求
3.2.1.1 .怎么组装?
3359 open.weixin.QQ.com/connect/QR connect? http://www.Sina.com/=http://www.Sina.com/http://www.Sina.com/response _ type=code 3358
! 这是伺服器传送给微信开放平台的,目的是回到二维码,让使用者扫描。
3.2.1.2 .怎么送,拿什么送?
在服务器上从其他网络获取资源一定是http客户端。 (请原谅我的见识太短。 请大家补充一下。 )。
打包微信登录;
import net.sf.json.JSONObject;
importorg.Apache.http.http entity;
importorg.Apache.http.httpresponse;
import org.apache.h
ttp.client.methods.HttpGet;import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
/***
* 微信扫码登录:工具类
*/
public class wexinUtils {
//参数
public static final String APPID = "";
public static final String APPSECRETID = "";
//回调地址
public static final String CALLBACK = "";
/***
*
* @param url : 组装发送请求的URL
* @return
* 该方法用于发送请求到微信开放平台,将返回值转化为json
*/
public static JSONObject getjson(String url) throws IOException {
//初始化JSONObject
JSONObject jsonObject = null;
//初始化HttpClient(该对象用于在服务器内部发送请求)
DefaultHttpClient client = new DefaultHttpClient();
//GET请求
HttpGet httpGet = new HttpGet(url);
//发送请求
HttpResponse response = client.execute(httpGet);
//获取返回结果
HttpEntity entity = response.getEntity();
//内容不为null
if(entity!=null){
//转化字符为为JSON
String result = EntityUtils.toString(entity,"UTF-8");
//转化为Json
jsonObject = jsonObject.fromObject(result);
}
//释放
httpGet.releaseConnection();
return jsonObject;
}
}
!这里回调地址,跟之前审核的哪个没有一点关系,这个URL是服务器真实存在的,并且能够访问得该资源.而哪个只是一个字符串,没有太多的作用(这是我的看法啦...,真实的希望各位大佬解释一下啦...)
3.3.1. 回调数据怎么获取?
经历上次请求后,如果用户选择了登录授权,那么微信开放平台就会将请求发到之前我们指定的URL去.这个时候就会有CODE返回了,通过这个Code与AppIDSecretID获取Access_token,就可以获取到OPENID,这个OPENID就是微信用户唯一标识,通过这个标识,我们又可以组装URL,进而获取当前微信用户信息.
package weixinlogin;import io.cite.CiteRuntime;import io.cite.plugin.cookie.cookie;import io.cite.plugin.database.db;import net.sf.json.JSONObject;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;import java.sql.SQLException;import java.util.Map;/*** * 微信登录成功回调,获取access_token */public class weixincallback { public void execute(CiteRuntime runtime) throws IOException, SQLException { //请求,响应对象 HttpServletRequest request = runtime.getRequest(); HttpServletResponse response = runtime.getResponse(); //获取Code String Code = request.getParameter("code"); //通过Code/AppID/SecretID获取access_token String url ="https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + wexinUtils.APPID + "&secret=" + wexinUtils.APPSECRETID + "&code=" + Code + "&grant_type=authorization_code"; //发送请求获取access_token JSONObject jsonobject = wexinUtils.getjson(url); //微信登录用户标识(用于处理业务) String openid = jsonobject.getString("openid"); //获取access_token String access_token = jsonobject.getString("access_token"); //组装获取用户信息URL String userinfor ="https://api.weixin.qq.com/sns/userinfo/access_token?appid=" + access_token + "&openid="+ openid + "&lang=zh_CN"; //发送请求,获取用户信息 JSONObject userinforobj = wexinUtils.getjson(userinfor); //业务处理... }}
最终结果: