融云后台服务创建token

来源:互联网 发布:淘宝店铺卖气模名字 编辑:程序博客网 时间:2024/05/01 17:22

public class RongUtils {private static final String APPKEY = "RC-App-Key";private static final String NONCE = "RC-Nonce";private static final String TIMESTAMP = "RC-Timestamp";private static final String SIGNATURE = "RC-Signature";public static String getToken(String id) {String nonce = String.valueOf(Math.random() * 1000000);String timestamp = String.valueOf(System.currentTimeMillis() / 1000);StringBuilder toSign = new StringBuilder("BIX27pwRm8Kd").append(nonce).append(timestamp);String sign = CodeUtil.hexSHA1(toSign.toString());Map<String, String> headers = new HashMap<String, String>();headers.put(APPKEY, "");headers.put(NONCE, nonce);headers.put(TIMESTAMP, timestamp);headers.put(SIGNATURE, sign);headers.put("Content-Type", "application/x-www-form-urlencoded");JSONObject json = JSON.parseObject(HttpUtils.post("http://api.cn.ronghub.com/user/getToken.json", "portraitUri=&name=&userId=" + id, headers).getContent());return json.getString("token");}}

1:生成TOKEN

2:SHA1

public class CodeUtil {public static String hexSHA1(String value) {try {MessageDigest md = MessageDigest.getInstance("SHA-1");md.update(value.getBytes("utf-8"));byte[] digest = md.digest();return byteToHexString(digest);} catch (Exception ex) {throw new RuntimeException(ex);}}public static String byteToHexString(byte[] bytes) {return String.valueOf(Hex.encodeHex(bytes));}}



1 0
原创粉丝点击