package io.agora.sample; import org.apache.commons.httpclient.HttpClient; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.Base64; import static cn.com.goldenwater.core.web.BaseController.buildFailResponse; import static cn.com.goldenwater.core.web.BaseController.buildSuccessResponse; public class Base64Encoding { private static final String APP_ID = "6f592a3023ef4960a2d5342a6fc456e7"; /** * 客户 ID */ private static final String CUSTOMER_KEY = "77864af0d5e449ae9b8271a930aba2f4"; /** * 客户密钥 */ private static final String CUSTOMER_SECRET = "216c9520cc974121806034bad52f533c"; public static void main(String[] args) throws IOException, InterruptedException { // 拼接客户 ID 和客户密钥并使用 base64 编码 String plainCredentials = CUSTOMER_KEY + ":" + CUSTOMER_SECRET; String base64Credentials = new String(Base64.getEncoder().encode(plainCredentials.getBytes(StandardCharsets.UTF_8))); // 创建 authorization header String authorizationHeader = "Basic " + base64Credentials; // 创建 HTTP 请求对象 try (CloseableHttpClient httpCilent = HttpClients.createDefault()) { HttpGet httpGet = new HttpGet("https://api.agora.io/dev/v1/channel/" + APP_ID); httpGet.setHeader("Content-Type", "application/json"); httpGet.setHeader("Authorization", authorizationHeader); HttpResponse httpResponse = httpCilent.execute(httpGet); System.out.println(EntityUtils.toString(httpResponse.getEntity())); } catch (IOException e) { e.printStackTrace(); } } }