欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

即时通信IM,服务端导入单个帐号v4/im_open_login_svc/account_import接口的调用

程序员文章站 2022-04-26 22:29:22
...

代码如下:

// 生成IM用户接口
private final static String URL = "https://console.tim.qq.com/v4/im_open_login_svc/account_import?sdkappid=XXXXXXX&identifier=XXXXXX&usersig=XXXXXXXXX&random=4192147295&contenttype=json";

解析:即时通信IM,服务端导入单个帐号v4/im_open_login_svc/account_import接口的调用
生成usersig见接口文档地址:https://cloud.tencent.com/document/product/269/32688
这个我以后会在文章中补上,用代码的方式生成usersig。

好了,接下来就是正儿八经的生成用户的代码了:

/**
	 * 生成IM用户
	 * @param args
	 * @throws IOException
	 * username 你要生成的用户名
	 * headurl 你用户的头像路径
	 */
	public static String imUser(String username,String headurl) throws IOException {
		// TODO Auto-generated method stub
		// 连接服务器
		HttpURLConnection connection = connection(URL);
		DataOutputStream out = new DataOutputStream(connection.getOutputStream());
		JSONObject obj = new JSONObject();
		obj.element("Identifier", username);
		obj.element("Nick", username);
		obj.element("FaceUrl", headurl);
		System.out.println(obj.toString());

		// 向腾讯请求传入编码为UTF-8格式的json数据
		out.write(obj.toString().getBytes("UTF-8"));

		out.flush();
		out.close();
		// 获得服务器返回的结果
		BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
		String lines;
		StringBuffer sb = new StringBuffer("");
		while ((lines = reader.readLine()) != null) {
			lines = new String(lines.getBytes(), "utf-8");
			sb.append(lines);
		}
		reader.close();
		System.out.println(sb);
		// 将BufferedReader转换为String 类型,然后进行解析
		String returnString = sb.toString();
		JsonObject jObject =new JsonParser().parse(returnString).getAsJsonObject();
		String ActionStatus = jObject.get("ActionStatus").getAsString();
		System.out.println(ActionStatus);
		return ActionStatus;
	}

public static HttpURLConnection connection(String URL) throws IOException {
		URL url = new URL(URL);
		HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		connection.setDoOutput(true);
		connection.setDoInput(true);
		connection.setRequestMethod("POST");
		connection.setUseCaches(false);
		connection.setInstanceFollowRedirects(true);
		connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
		connection.connect();
		return connection;
		// TODO Auto-generated method stub
	}

测试

public static void main(String[] args) throws IOException {
        // 要导入的用户名
		String username = "zx0062";
		// 你想要的图片路径
		String headurl = "http://********:8088/*******/webpage/auto/userimg/216140998.jpeg";
		imUser(username, headurl);
	}

运行
即时通信IM,服务端导入单个帐号v4/im_open_login_svc/account_import接口的调用

相关标签: 腾讯云