发表于: 2017-12-30 17:35:40
3 464
今天完成任务:
一.终于完成了微信的接入
获取到了用户openid,昵称,还有头像.
所以记录一下.首先是要自己申请一个测试号.然后微信关注这个公众号.
这个就是申请的测试号.然后页面拉到最下面.左边的选择栏中选择开发者工具.
选择公众平台测试号.然后开通一个自己测试的账号.
查看自己的appid和appsecret.然后自己的微信关注测试号.作为一个用户来使用.
这里面做好了以后,就要去查看微信的开发者文档.里面内容很多,但是我们就只用一小部分.选择 微信网页开发----微信网页授权
这个页面下面是微信授权的流程.在微信上面的网页访问之前必须要有微信的授权,才可以跳转到自己的页面.所以第一步就是先去获取到这个授权,并且在授权最后可以获取到用户的openid和用户信息.
仔细读一下前面的介绍内容,然后就可以开始做授权.暂时只会用到第一二,四步.
第一步同意授权.
这里面就是要先把这个url复制下来,然后对应的参数需要填好.每个参数作用文档里面都有讲.
redirect_url 这里是需要自己设置一下自己的域名或者服务的ip.并且在这个拼接的url里面需要加上http://,不然会报错.
就像这样
https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=http://www.baidu.com&response_type=code&scope=SCOPE&state=STATE#wechat_redirect
.拼接好了以后就需要有一个自己的项目,新建一个web项目,然后需要有一个接口,里面需要自己设置跳转到刚刚拼接的url里面.
@RequestMapping(value = "/a/wechat/aa",method = RequestMethod.GET)
public String demo(){
String urll="https://open.weixin.qq.com/connect/oauth2/authorize?" +
"appid=APPID" +
"&redirect_uri=http://hushanxing.net/a/home" +
"&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
return "redirect:"+urll;
}
有了这个以后还需要有一个对应的跳转以后的接口.这个接口是redirect_url里面的接口,是用来获取微信的回调信息/a/home
在回调的接口里面可以获取到code..
第二步是通过code获取access_token和openid
获取到code以后.需要再继续发送HTTP请求.并且接收到返回的一个json对象.这里面是包含了access_token和openid.
这里需要有一个发送http请求,并且接受返回的Response的方法.输入url,返回一个json对象.
public static JSONObject sendGetRequest(String url){
HttpClient httpClient = new DefaultHttpClient();
HttpEntity httpEntity=null;
String content=null;
JSONObject jsonObject=null;
try {
HttpGet request = new HttpGet(url);
HttpResponse response = httpClient.execute(request);
System.out.println("响应"+response);
httpEntity=response.getEntity();
String aa=EntityUtils.toString(httpEntity);
jsonObject=JSONObject.parseObject(aa);
System.out.println("返回的json是"+jsonObject);
} catch (IOException e) {
e.printStackTrace();
System.out.println("请求失败");
}
return jsonObject;
}
第三步:
获取到了access_token和openid以后,就需要继续的发送请求,接收到json对象.这次返回的就是用户的信息了.
这里面也是同样的要用上面的工具类发送请求...接受返回值.
下面这个redirect_url里面接口的内容..就是上面提到的内容.
@RequestMapping(value = "/a/home")public String home(HttpServletRequest request){
//获取code
String code=request.getParameter("code");
System.out.println("get code is"+code);
//通过code获取到用户的openid和access token
String codeUrl="https://api.weixin.qq.com/sns/oauth2/access_token?" +
"appid=APPID" +
"&secret=APPSECRET" +
"&code="+code+
"&grant_type=authorization_code";
JSONObject jsonObject= HttpRequest.sendGetRequest(codeUrl);
String access_token=jsonObject.getString("access_token");
String openid=jsonObject.getString("openid");
System.out.println(jsonObject);
System.out.println("access_token is %n"+access_token+"open id is "+openid);
//通过获取到的openid和accesstoken换区用户的信息
String user_infoUrl="https://api.weixin.qq.com/sns/userinfo?" +
"access_token="+access_token+
"&openid="+openid +
"&lang=zh_CN";
JSONObject jsonObject1=HttpRequest.sendGetRequest(user_infoUrl);
String nickname=jsonObject1.getString("nickname");
String sex=jsonObject1.getString("sex");
String province=jsonObject1.getString("province");
String headimgurl=jsonObject1.getString("headimgurl");
System.out.println(jsonObject1);
System.out.println("get sex is "+sex+"get nickname is "+nickname+"get province is"+province+"get headImg is"+headimgurl);
return "wechat";
}
最后全部完成之后,微信的界面会跳转到自己指向的界面.就是上面写的return "WeChat"这是个jsp.测试用的.
大概就是这样.可以获取到微信的用户信息了.然后跳转到自己设置的界面.
微信的文档虽然看起来很累,但是也只能慢慢看,并且多看几遍就自己会了.没那么难的.
明天计划:
明天准备部署新的服务器.重构代码就好了.
学习基础.
遇到问题:
项目延期...
微信里面一些坑.
收获:
学会了一点点微信...
评论