4bec2d1efe6a85c8319a8702015e5242f9bc43b6.svn-base 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package io.agora.sample;
  2. import org.apache.commons.httpclient.HttpClient;
  3. import org.apache.http.HttpResponse;
  4. import org.apache.http.client.methods.HttpGet;
  5. import org.apache.http.impl.client.CloseableHttpClient;
  6. import org.apache.http.impl.client.HttpClients;
  7. import org.apache.http.util.EntityUtils;
  8. import java.io.IOException;
  9. import java.nio.charset.StandardCharsets;
  10. import java.util.Base64;
  11. import static cn.com.goldenwater.core.web.BaseController.buildFailResponse;
  12. import static cn.com.goldenwater.core.web.BaseController.buildSuccessResponse;
  13. public class Base64Encoding {
  14. private static final String APP_ID = "6f592a3023ef4960a2d5342a6fc456e7";
  15. /**
  16. * 客户 ID
  17. */
  18. private static final String CUSTOMER_KEY = "77864af0d5e449ae9b8271a930aba2f4";
  19. /**
  20. * 客户密钥
  21. */
  22. private static final String CUSTOMER_SECRET = "216c9520cc974121806034bad52f533c";
  23. public static void main(String[] args) throws IOException, InterruptedException {
  24. // 拼接客户 ID 和客户密钥并使用 base64 编码
  25. String plainCredentials = CUSTOMER_KEY + ":" + CUSTOMER_SECRET;
  26. String base64Credentials = new String(Base64.getEncoder().encode(plainCredentials.getBytes(StandardCharsets.UTF_8)));
  27. // 创建 authorization header
  28. String authorizationHeader = "Basic " + base64Credentials;
  29. // 创建 HTTP 请求对象
  30. try (CloseableHttpClient httpCilent = HttpClients.createDefault()) {
  31. HttpGet httpGet = new HttpGet("https://api.agora.io/dev/v1/channel/" + APP_ID);
  32. httpGet.setHeader("Content-Type", "application/json");
  33. httpGet.setHeader("Authorization", authorizationHeader);
  34. HttpResponse httpResponse = httpCilent.execute(httpGet);
  35. System.out.println(EntityUtils.toString(httpResponse.getEntity()));
  36. } catch (IOException e) {
  37. e.printStackTrace();
  38. }
  39. }
  40. }