java服务器端集成环信好友体系_Java IM环信服务端集成之用户体系集成(二)

package com.xunxin.controller.app.huanxin;

import java.util.ArrayList;

import java.util.List;

import org.apache.log4j.Logger;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import com.alibaba.fastjson.JSONObject;

import com.xunxin.controller.system.BaseController;

import com.xunxin.dao.sys.PlatformMutualityManagentDao;

import com.xunxin.util.HttpUtils;

import com.xunxin.util.HttpUtils.UHeader;

import com.xunxin.vo.sys.PageData;

import com.xunxin.vo.sys.PlatformMutualityManagent;

import com.xunxin.web.api.bean.Response;

import com.xunxin.web.api.bean.Router;

/**

* Copyright © 2017 noseparte(Libra) © Like the wind, like rain

* @Author Noseparte

* @Compile 2017年10月24日 -- 下午5:31:38

* @Version 1.0

* @Description 获取环信SDK的返回信息

*/

@Controller

@RequestMapping(value=Router.PATH+Router.Easemob.PATH)

public class EasemobController extends BaseController{

private static final Logger log = Logger.getLogger(EasemobController.class);

private static final String LINKED_URL = "http://www.easemob.com/";

@Autowired

private PlatformMutualityManagentDao platformMutualityManagentDao;

@RequestMapping(value=Router.Easemob.GET_TOKEN,method=RequestMethod.POST)

@ResponseBody

public Response getToken(PageData pd) {

log.info("info-: get huanxin token begin");

Response res = this.getReponse();

try {

PlatformMutualityManagent pm = platformMutualityManagentDao.findOne(pd);

String app_key = pm.getApp_key();

String org_name = app_key.split("#")[0];//组织名称

String app_name = app_key.split("#")[1];//APP名称

String client_id = app_key.split("#")[1];//客户端id

String client_secret = app_key.split("#")[1];//客户端密钥

//post请求路径

String url = LINKED_URL + org_name + "/" + app_name + "/" + "token";

//post请求参数 json

StringBuffer sb = new StringBuffer();

sb.append("grant_type:").append("client_credentials");

sb.append("client_id:").append(client_id);

sb.append("client_secret:").append(client_secret);

String argJson = sb.toString();

//请求头信息

List headerList = new ArrayList();

headerList.add(new UHeader("Content-Type", "application/json"));

//获取环信token

String resp = HttpUtils.getPostResponse(url, argJson, headerList);

JSONObject obj = JSON.parseObject(resp);

//TODO 判断response 状态码

String access_token = obj.getString("access_token"); //token 值

String expires_in = obj.getString("expires_in"); //token 有效时间,以秒为单位,在有效期内不需要重复获取

String application = obj.getString("application"); //token 有效时间,以秒为单位,在有效期内不需要重复获取

//获得返回值 并解析

PageData tokenPd = new PageData<>();

tokenPd.put("access_token", access_token);

tokenPd.put("expires_in", expires_in);

tokenPd.put("application", application);

log.info("infoMsg:- get token success, ok");

return res.success(tokenPd);

} catch (Exception e) {

log.error("errorMsg:- An error occurred when get token!");

return res.failure(e.getMessage());

}

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值