java服务端调用环信接口

调用环信api需要获取环信的token

public class Token {

    private String access_token; //有效的token字符串
    private String expires_in;   //token 有效时间,以秒为单位,在有效期内不需要重复获取
    private String application;//当前 App 的 UUID 值

    public String getAccess_token() {
        return access_token;
    }

    public void setAccess_token(String access_token) {
        this.access_token = access_token;
    }

    public String getExpires_in() {
        return expires_in;
    }

    public void setExpires_in(String expires_in) {
        this.expires_in = expires_in;
    }

    public String getApplication() {
        return application;
    }

    public void setApplication(String application) {
        this.application = application;
    }
}

//下面为获取环信token、获取群组、消息推送的调用方式(均使用restful形式)

import com.alibaba.fastjson.JSONObject; 
import com.fasterxml.jackson.databind.node.JsonNodeFactory; 
import com.fasterxml.jackson.databind.node.ObjectNode; 
import org.springframework.http.*; 
import org.springframework.web.client.RestClientException; 
import org.springframework.web.client.RestTemplate;

public class HuanXin001 {

    private static RestTemplate restTemplate = new RestTemplate();

    private static final String ORG_NAME = "11...336";//企业的唯一标识,开发者在环信开发者管理后台注册账号时填写的企业 ID
    private static final String CLIENT_ID = "YX...Rjg";//App的client_id
    private static final String CLIENT_SECRET = "YXA6-S...OQ";//App的client_secret
    private static final String APP_NAME = "im001";//同一企业下'APP'唯一标识,在环信开发者管理后台创建应用时填的应用名称
    private static final String URL_PREFIX = "http://a1.easemob.com/" + ORG_NAME + "/" + APP_NAME + "/";//链接前缀


    //获取环信token


    public static Token getToken() {
        try {
            String url = URL_PREFIX + "token";
            JSONObject body = new JSONObject();
            body.put("grant_type", "client_credentials");
            body.put("client_id", CLIENT_ID);
            body.put("client_secret", CLIENT_SECRET);

            HttpEntity httpEntity = new HttpEntity(body.toString(), null);
            ResponseEntity<Token> tokenResponseEntity = restTemplate.postForEntity(url, httpEntity, Token.class);

            return tokenResponseEntity.getBody();
        } catch (RestClientException e) {
            e.printStackTrace();
            return null;
        }
    }

    //推送消息


    public static void sendMes(){
        String url = URL_PREFIX + "messages";
        Token token = getToken();
        String _token = token.getAccess_token();

        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        headers.add("Authorization","Bearer "+_token);//

        JSONObject body = new JSONObject();
        body.put("target_type", "chatgroups");
        body.put("target",new String[]{"941...873","778..4881","7161..2817"});

        Map jsonNodes = new HashMap();

        jsonNodes.put("type","txt");
        jsonNodes.put("msg","asb1234567890");

        body.put("msg",jsonNodes);
        body.put("from","智能小蜜");//推送的昵称显示,可app设置

        HttpEntity httpEntity = new HttpEntity(body.toString(), headers);
        ResponseEntity tokenResponseEntity = restTemplate.postForEntity(URL_PREFIX + "messages", httpEntity,null);

        System.out.println("发送消息==="+tokenResponseEntity.getStatusCode());
    }

    //获取所有的群组


    public static void getQunzus() throws Exception{

        Token token = getToken();
        String _token = token.getAccess_token();

        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        headers.add("Authorization","Bearer "+_token);//

        String url = URL_PREFIX + "chatgroups";

        HttpEntity<String> httpEntity = new HttpEntity<String>(headers);

        ResponseEntity objectResponseEntity = restTemplate.exchange(url, HttpMethod.GET,httpEntity,String.class);

        System.out.println("获得的群组为="+objectResponseEntity.getBody());
    }

    
    //测试方法..
    public static void main(String[] args) throws Exception{
        //Token token = getToken();
        //System.out.println("token="+token.getAccess_token());
        //getQunzus();
        sendMes();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值