微信小程序 auth.getAccessToken 获取access_token JAVA

一、认清auth.getAccessToken

        这个接口不是wx.request()之类的在小程序端使用,它在服务器中编写代码使用,是一种请求规范。向微信服务器请求access_token时必须遵守的规范。即:(1)请求方式为GET (2)请求网址为https://api.weixin.qq.com/cgi-bin/token?(3)必须附带三个参数:grant_type,appid,secret.满足以上条件可以向服务器申请获得access_token,因而需要掌握网络请求相关的代码知识。

二、代码实现(JAVA为例)

   (1)编写HttpRequest工具类

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
101
102
103
104
105
106
107
108
109
110
package com.data.service;
 
 
import java.io.BufferedReader;
 
import java.io.IOException;
 
import java.io.InputStreamReader;
 
import java.io.OutputStreamWriter;
 
import java.io.PrintWriter;
 
import java.net.URL;
 
import java.net.URLConnection;
 
import java.util.List;
 
import java.util.Map;
public class HttpRequest {
       
     
     public  String sendGet(String url, String param) {
 
            String result = "";
 
            BufferedReader in = null;
 
            try {
 
                String urlNameString = url + "?" + param;
 
                URL realUrl = new URL(urlNameString);
 
                // 打开和URL之间的连接
 
                URLConnection connection = realUrl.openConnection();
 
                // 设置通用的请求属性
 
                connection.setRequestProperty("accept", "*/*");
 
                connection.setRequestProperty("connection", "Keep-Alive");
 
                connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
 
                // 建立实际的连接
 
                connection.connect();
 
                // 获取所有响应头字段
 
                Map<String, List<String>> map = connection.getHeaderFields();
 
                // 遍历所有的响应头字段
 
                for (String key : map.keySet()) {
 
                    // System.out.println(key + "--->" + map.get(key));
 
                }
 
                // 定义 BufferedReader输入流来读取URL的响应
 
                in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
 
                // in = new BufferedReader(new
                // InputStreamReader(connection.getInputStream()));
 
                String line;
 
                while ((line = in.readLine()) != null) {
 
                    result += line;
 
                }
 
            } catch (Exception e) {
 
                System.out.println("发送GET请求出现异常!" + e);
 
                e.printStackTrace();
 
            }
 
            // 使用finally块来关闭输入流
 
            finally {
 
                try {
 
                    if (in != null) {
 
                        in.close();
 
                    }
 
                } catch (Exception e2) {
 
                    e2.printStackTrace();
 
                }
 
            }
 
            return result;
 
        }
}

  (2调用工具类发起请求:

     

1
2
3
4
5
6
7
8
9
10
11
import com.data.service.HttpRequest;
import net.sf.json.JSONObject;
String APP_ID=request.getParameter("id");
String APPSECRET=request.getParameter("secret");
String grant_type="client_credential";
HttpRequest requestion=new HttpRequest();
String params = "grant_type=" + grant_type + "&secret=" + APPSECRET + "&appid="+ APP_ID;
String sendGet = requestion.sendGet("https://api.weixin.qq.com/cgi-bin/token", params);
JSONObject json = JSONObject.fromObject(sendGet);
 String accesstoken = (String) json.get("access_token");
//accesstoken即为申请得到的access_token(这个值两小时就会失效  2022)

  引用:https://blog.csdn.net/weixin_41716049/article/details/84066112

                 https://www.jianshu.com/p/70d5f80c16b8

         以上看法为个人所见,欢迎指正。

 
posted @   浩楠说不谢  阅读(2244)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Blazor Hybrid适配到HarmonyOS系统
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· 解决跨域问题的这6种方案,真香!
· 分享4款.NET开源、免费、实用的商城系统
· 一套基于 Material Design 规范实现的 Blazor 和 Razor 通用组件库
点击右上角即可分享
微信分享提示