java调用微信服务号API,根据openid发送消息的代码。

微信公众号 java 文章 2023-03-20 09:20 1152 0 全屏看文

AI助手支持GPT4.0

为了使用Java调用微信服务号API以根据openid发送消息,你需要首先获取微信公众平台的access_token。以下是一段示例代码,展示了如何根据openid发送文本消息。


首先,在Java项目中添加以下依赖:

<!-- Maven -->
<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp</artifactId>
    <version>4.9.3</version>
</dependency>


<!-- Gradle -->

implementation 'com.squareup.okhttp3:okhttp:4.9.3'

接下来,创建一个Java类,用于调用微信API:

import okhttp3.*;

import java.io.IOException;

public class WeChatService {

    private static final String APP_ID = "your_app_id";
    private static final String APP_SECRET = "your_app_secret";
    private static final String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + APP_ID + "&secret=" + APP_SECRET;
    private static final String SEND_MESSAGE_URL = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=";

    public static void main(String[] args) {
        try {
            String accessToken = getAccessToken();
            String openid = "user_openid";
            String message = "Hello, this is a message from WeChat Service Account!";
            sendMessage(accessToken, openid, message);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static String getAccessToken() throws IOException {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(ACCESS_TOKEN_URL).build();
        Response response = client.newCall(request).execute();
        String responseBody = response.body().string();
        // 此处需要解析JSON,获取access_token。你可以使用Gson、Jackson等库进行解析。
        String accessToken = parseAccessToken(responseBody);
        return accessToken;
    }

    private static void sendMessage(String accessToken, String openid, String message) throws IOException {
        OkHttpClient client = new OkHttpClient();
        MediaType JSON = MediaType.parse("application/json; charset=utf-8");
        String jsonMessage = createJsonMessage(openid, message);

        RequestBody body = RequestBody.create(JSON, jsonMessage);
        Request request = new Request.Builder().url(SEND_MESSAGE_URL + accessToken).post(body).build();
        Response response = client.newCall(request).execute();
        System.out.println("Send message response: " + response.body().string());
    }

    private static String createJsonMessage(String openid, String content) {
        return "{%"touser%":%"" + openid + "%",%"msgtype%":%"text%",%"text%":{%"content%":%"" + content + "%"}}";
    }

    // 用于解析access_token的方法,你需要根据实际情况实现此方法
    private static String parseAccessToken(String responseBody) {
        // 使用你选择的JSON解析库解析responseBody,获取access_token
        // 示例:return jsonObject.getString("access_token");
        return "";
    }
}

请注意,你需要替换APP_ID和APP_SECRET为你自己的应用ID和密钥,以及替换user_openid为实际的用户openid。此代码示例仅发送文本消息,你可以根据需要修改createJsonMessage方法以发送其他类型的消息。同时,你需要实现parseAccessToken方法以从API响应中解


-EOF-

AI助手支持GPT4.0