package com.newfortune.yx.admin.controller;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.TreeMap;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import com.tencentcloudapi.wemeet.common.profile.HttpProfile;
import com.tencentcloudapi.wemeet.models.MeetingSetting;
import com.tencentcloudapi.wemeet.models.User;
import com.tencentcloudapi.wemeet.models.meeting.CreateMeetingRequest;
import net.sf.json.JSONObject;
public class test12 {
private static final String SECRET_ID = "nOIhd9JQeV5NmRTuKKVyiaDun889F1Gh";//自己的SecretId
private static final String SECRET_KEY = "U2HPAnTeK7mQ2zGmvr2vIAsPDIWBi5UXn2oZ6pu0mBxti06X";//自己的SecretKey
private static final String APP_ID="210648508";//自己的APPID
private static final String skid_ID="18510100782";
private static final String CONTENT_CHARSET = "UTF-8";
private static final String HMAC_ALGORITHM = "HmacSHA256";
private static char[] HEX_CHAR = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
public static void main(String[] args) throws Exception {
TreeMap<String, String> params = new TreeMap<String, String>();
String secretId=SECRET_ID;
String secretKey=SECRET_KEY;
String timestamp=String.valueOf(System.currentTimeMillis()/1000);
String nonce=String.valueOf(new Random().nextInt(java.lang.Integer.MAX_VALUE));
// System.out.println("timestamp="+timestamp);
// System.out.println("nonce="+nonce);
String appId=APP_ID;
String method="POST";
String url="https://api.meeting.qq.com/v1/meetings/";
String meeting_id = "2296396612218194962";
String url2 = meeting_id + "/enroll/import";
url = url + url2 ;
String uri="/v1/meetings";
Date today= new Date();
Calendar c = Calendar.getInstance();
c.setTime(new Date());//今天
c.add(Calendar.DAY_OF_MONTH,1);
Date tomorrow=c.getTime();//明天
int createTime = getSecondTimestamp(today);
int end_time = getSecondTimestamp(tomorrow);
JSONObject requestParam = new JSONObject();
requestParam.put("operator_id", "62ae2a42e2a547bc9861bd8b8fabe080") ;
requestParam.put("operator_id_type", 1) ;
requestParam.put("instanceid", 1) ;
List<JSONObject> enroll_list = new ArrayList<>();
JSONObject paramInfo = new JSONObject();
paramInfo.put("userid", "Adminkai");
// paramInfo.put("area", "86");
// paramInfo.put("phone_number", "18579932745");
// paramInfo.put("nick_name", "一水");
enroll_list.add(paramInfo);
requestParam.put("enroll_list", enroll_list) ;
String req_body = requestParam.toString() ;
String mkString=httpSign(secretKey,method,secretId,nonce,timestamp,uri,req_body);
System.out.println("加密后密钥"+mkString);
requestPost(url,secretId,timestamp,nonce,appId,mkString,req_body,skid_ID);
}
public static String requestPost(String url,String secretId,String timestamp,String nonce,String appId,String mkString,String req_body,String skid_ID){
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
String jsonStr = "";
try {
httpPost.setHeader("X-TC-Key",secretId);
httpPost.setHeader("AppId", appId);
httpPost.setHeader("SdkId", skid_ID);//有就加,没有可以不加
httpPost.setHeader("X-TC-Nonce",nonce);
httpPost.setHeader("X-TC-Timestamp",timestamp);
httpPost.setHeader("X-TC-Signature", mkString);
httpPost.setHeader("content-type", "application/json");
httpPost.setHeader("X-TC-Registered", "1");
httpPost.setHeader("Body",req_body);
httpPost.setEntity(new StringEntity(req_body));
CloseableHttpResponse httpResponse = null;
httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
jsonStr = EntityUtils.toString(httpEntity,"UTF-8");
}
System.out.println("返回信息"+jsonStr);
} catch (IOException e) {
}finally{
httpPost.releaseConnection();
try {
httpClient.close();
} catch (IOException e) {
}
}
return jsonStr;
}
public static int getSecondTimestamp(Date date){
if (null == date) {
return 0;
}
String timestamp = String.valueOf(date.getTime());
int length = timestamp.length();
if (length > 3) {
return Integer.valueOf(timestamp.substring(0,length-3));
} else {
return 0;
}
}
/**
* 生成签名,开发版本oracle jdk 1.8.0_221
* @param requestBody 请求体,没有的设为空串
* @return 签名,需要设置在请求头X-TC-Signature中
* @throws NoSuchAlgorithmException e
* @throws InvalidKeyException e
*/
public static String httpSign(String secret, String method, String key, String nonce, String timestamp, String uri,String requestBody)
throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException {
String sig="";
try{
String HTTPMethod =method;
String Headers ="X-TC-Key=" + key + "&X-TC-Nonce=" + nonce + "&X-TC-Timestamp=" + timestamp;
// String Headers ="X-TC-Nonce=" + nonce + "&X-TC-Timestamp=" + timestamp + "&X-TC-Key=" + key;
String URI=uri;
String Params=requestBody;
String tobeSig=HTTPMethod + "\n" + //POST, GET
Headers + "\n" +
URI + "\n" + //eg: https://api.meeting.qq.com/v1/meetings, URI=/v1/meetings
Params ;
Mac mac = Mac.getInstance(HMAC_ALGORITHM);
SecretKeySpec secretKeySpec = new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), mac.getAlgorithm());
mac.init(secretKeySpec);
byte[] hash = mac.doFinal(tobeSig.getBytes(StandardCharsets.UTF_8));
String hexHash = bytesToHex(hash);
return new String(Base64.getEncoder().encode(hexHash.getBytes(StandardCharsets.UTF_8)));
}catch (Exception e){
e.printStackTrace();
}
return sig;
}
static String bytesToHex(byte[] bytes) {
char[] buf = new char[bytes.length * 2];
int index = 0;
for (byte b : bytes) {
buf[index++] = HEX_CHAR[b >>> 4 & 0xf];
buf[index++] = HEX_CHAR[b & 0xf];
}
return new String(buf);
}
public static String getUnicode(String s) {
try {
StringBuffer out = new StringBuffer("");
byte[] bytes = s.getBytes("unicode");
for (int i = 0; i < bytes.length - 1; i += 2) {
out.append("\\u");
String str = Integer.toHexString(bytes[i + 1] & 0xff);
for (int j = str.length(); j < 2; j++) {
out.append("0");
}
String str1 = Integer.toHexString(bytes[i] & 0xff);
out.append(str1);
out.append(str);
}
return out.toString();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
}
}
相似问题