您的位置:首页 > 移动开发 > 微信开发

微信授权登录

2017-01-17 16:58 761 查看
没有公众号高级接口权限可以先用微信公众平台接口测试账号

公众测试账号

1,登录后,填写回调域名



2,引导用户点击授权链接

微信授权开发文档详细说明地址

https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect



1 //授权成功回调域名获取code
2     public void oauthProsperity(Controller con){
3         //用户授权获取的code
4         String appId = PropKit.get("appId");
5         String appsecret = PropKit.get("appsecret");
6         String code = con.getPara("code");
7         HttpClientBuilder.create().build();
8         if(code!=null && code.trim()!=""){
9             TokenModel token = HttpClientUtil.getOauth2AccessToken(appId, appsecret, code);
10             WeixinUserModel userInfo = HttpClientUtil.getUserInfo(token.getStr("accessToken"), token.getStr("openId"));
11             con.renderJson(userInfo.toJson());
12             return;
13         }
14         con.renderJson();
15     }




/**
* 通过code换取网页授权access_token,
* @param appId  公众号的唯一标识
* @param appsecret  公众号的appsecret
* @param code
* @return
*/
public static TokenModel getOauth2AccessToken(String appId,String appsecret,String code){
TokenModel token = null;//map
String url = "https://api.weixin.qq.com/sns/oauth2/access_token";
Map<String,String> params = new HashMap<String,String>();
params.put("appId", appId);
params.put("secret", appsecret);
params.put("code", code);
params.put("grant_type", "authorization_code");
JSONObject json = HttpClientUtil.httpPost(url, params);
if(json!=null){
token = new TokenModel();
token.set("openId", json.getString("openid"));
token.set("scope", json.getString("scope"));
token.set("accessToken", json.getString("access_token"));
token.set("expiresIn", json.getInt("expires_in"));
token.set("refreshToken", json.getString("refresh_token"));
token.save();
}
return token;
}


1 /**
2      * 拉取用户信息
3      * @param accessToken   网页授权接口调用凭证
4      * @param openId
5      * @return
6      */
7     public static WeixinUserModel getUserInfo(String accessToken,String openId){
8         WeixinUserModel model = null;//map
9         String url = "https://api.weixin.qq.com/sns/userinfo";
10         Map<String,String> map = new HashMap<String,String>();
11         map.put("ACCESS_TOKEN", accessToken);
12         map.put("OPENID", openId);
13         JSONObject json = HttpClientUtil.httpPost(url, map);
14         if(null!=json){
15             model = new WeixinUserModel();
16             model.set("id", json.getString("openid"));
17             model.set("subscribe", 1);
18             model.set("nickname", json.getString("nickname"));
19             model.set("sex", json.getInt("sex"));
20             model.set("province", json.getString("province"));
21             model.set("city", json.getString("city"));
22             model.set("country", json.getString("country"));
23             model.set("headimgurl", json.getString("headimgurl"));
24             //model.set("unionId", json.getString("unionid"));
25             model.save();
26         }
27         return model;
28     }


1 package com.rollDice.util;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.List;
6 import java.util.Map;
7
8 import org.apache.http.HttpEntity;
9 import org.apache.http.HttpStatus;
10 import org.apache.http.NameValuePair;
11 import org.apache.http.client.entity.UrlEncodedFormEntity;
12 import org.apache.http.client.methods.CloseableHttpResponse;
13 import org.apache.http.client.methods.HttpPost;
14 import org.apache.http.impl.client.CloseableHttpClient;
15 import org.apache.http.impl.client.HttpClientBuilder;
16 import org.apache.http.message.BasicNameValuePair;
17 import org.apache.http.util.EntityUtils;
18
19 import com.rollDice.model.TokenModel;
20 import com.rollDice.model.WeixinUserModel;
21
22 import net.sf.json.JSONObject;
23
24 public class HttpClientUtil {
25     public static CloseableHttpClient createDefault(){
26         return HttpClientBuilder.create().build();
27     }
28     /**
29      * 发送post请求
30      * @param url   链接
31      * @param params  参数
32      * @return
33      */
34     public static JSONObject httpPost(String url,Map<String,String> params){
35         CloseableHttpClient httpClient = createDefault();
36         HttpPost method = new HttpPost(url);
37         //拼接参数
38         List<NameValuePair> list = new ArrayList<NameValuePair>();
39         if(params!=null){
40             for(Map.Entry<String, String> entry : params.entrySet()){
41                 String key = entry.getKey().toString();
42                 String value = entry.getValue().toString();
43                 NameValuePair pair = new BasicNameValuePair(key,value);
44                 list.add(pair);
45             }
46         }
47         CloseableHttpResponse response = null;
48         try{
49             method.setEntity(new UrlEncodedFormEntity(list));
50             response = httpClient.execute(method);
51         }catch(Exception e){
52             e.printStackTrace();
53         }
54         //请求成功,得到响应
55         JSONObject jsonObject = null;
56         if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
57             HttpEntity httpEntity = response.getEntity();
58             String result = null;
59             try{
60                 result = EntityUtils.toString(httpEntity);
61             }catch(Exception e){
62                 e.printStackTrace();
63             }
64             //返回json格式
65             jsonObject = JSONObject.fromObject(result);
66         }
67         return jsonObject;
68     }


http和JSONObject相关jar下载



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: