admin
2024-06-29 7ac0b5be02902a96bd1feb658e41a9b69fa50738
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package com.taoke.autopay.utils;
 
import com.google.gson.Gson;
import com.taoke.autopay.dto.WXAppInfoDto;
import net.sf.json.JSONObject;
import org.yeshi.utils.HttpUtil;
import org.yeshi.utils.entity.wx.WXAPPInfo;
 
/**
 * @author hxh
 * @title: WxApiUtil
 * @description: 微信帮助类
 * @date 2024/6/28 17:27
 */
public class WxApiUtil {
 
    public class WXAccessTokenInfo {
        private String access_token;
        private int expires_in;
        private String refresh_token;
        private String openid;
        private String scope;
        private int is_snapshotuser;
        private String unionid;
 
        public String getAccess_token() {
            return access_token;
        }
 
        public void setAccess_token(String access_token) {
            this.access_token = access_token;
        }
 
        public int getExpires_in() {
            return expires_in;
        }
 
        public void setExpires_in(int expires_in) {
            this.expires_in = expires_in;
        }
 
        public String getRefresh_token() {
            return refresh_token;
        }
 
        public void setRefresh_token(String refresh_token) {
            this.refresh_token = refresh_token;
        }
 
        public String getOpenid() {
            return openid;
        }
 
        public void setOpenid(String openid) {
            this.openid = openid;
        }
 
        public String getScope() {
            return scope;
        }
 
        public void setScope(String scope) {
            this.scope = scope;
        }
 
        public int getIs_snapshotuser() {
            return is_snapshotuser;
        }
 
        public void setIs_snapshotuser(int is_snapshotuser) {
            this.is_snapshotuser = is_snapshotuser;
        }
 
        public String getUnionid() {
            return unionid;
        }
 
        public void setUnionid(String unionid) {
            this.unionid = unionid;
        }
    }
 
 
    public static WXAccessTokenInfo getAcessTokenInfo(String code, WXAppInfoDto app) throws Exception {
        String tokenUrl = String.format("https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code", app.getAppId(), app.getAppSecret(), code);
        String result = HttpUtil.get(tokenUrl);
        System.out.println(result);
        JSONObject root = JSONObject.fromObject(result);
        if (root.optInt("errcode", 0) != 0) {
            throw new Exception(root.optString("errmsg"));
        }
        return JsonUtil.getSimpleGson().fromJson(result, WXAccessTokenInfo.class);
    }
 
    public static void main(String[] args) throws Exception {
 
        getAcessTokenInfo("061KT5ll2vgWHd4VyIll2UVQZV0KT5lP", new WXAppInfoDto("wx6217429129959b05", "14ae1808a271111954c509d8cb06df92"));
    }
 
}