微信小程序 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工具类

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调用工具类发起请求:

     

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 @ 2022-02-23 18:30  浩楠说不谢  阅读(2144)  评论(0编辑  收藏  举报