BytedanceMediaClient.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package com.nuojing.media.auth.client;
  2. import com.fasterxml.jackson.databind.JsonNode;
  3. import com.fasterxml.jackson.databind.ObjectMapper;
  4. import com.nuojing.admin.common.BusinessException;
  5. import com.nuojing.media.auth.domain.entity.MediaAuthConfig;
  6. import lombok.RequiredArgsConstructor;
  7. import org.springframework.http.HttpEntity;
  8. import org.springframework.http.HttpHeaders;
  9. import org.springframework.http.HttpMethod;
  10. import org.springframework.http.MediaType;
  11. import org.springframework.http.ResponseEntity;
  12. import org.springframework.stereotype.Component;
  13. import org.springframework.util.StringUtils;
  14. import org.springframework.web.client.RestTemplate;
  15. import org.springframework.web.util.UriComponentsBuilder;
  16. import java.util.HashMap;
  17. import java.util.Map;
  18. @Component
  19. @RequiredArgsConstructor
  20. public class BytedanceMediaClient {
  21. private static final String TOKEN_URL = "https://ad.oceanengine.com/open_api/oauth2/access_token/";
  22. private static final String ADVERTISER_LIST_URL = "https://ad.oceanengine.com/open_api/oauth2/advertiser/get/";
  23. private static final String USER_INFO_URL = "https://api.oceanengine.com/open_api/2/user/info/";
  24. private static final String CC_ADVERTISER_LIST_URL = "https://api.oceanengine.com/open_api/2/customer_center/advertiser/list/";
  25. private final ObjectMapper objectMapper;
  26. private final RestTemplate restTemplate = new RestTemplate();
  27. public JsonNode exchangeToken(MediaAuthConfig config, String authCode) {
  28. Map<String, Object> body = new HashMap<>();
  29. body.put("app_id", config.getAppId());
  30. body.put("secret", config.getAppSecret());
  31. body.put("auth_code", authCode);
  32. return postJson(TOKEN_URL, body);
  33. }
  34. public JsonNode advertiserList(String accessToken) {
  35. String url = UriComponentsBuilder.fromUriString(ADVERTISER_LIST_URL)
  36. .queryParam("access_token", accessToken)
  37. .build()
  38. .encode()
  39. .toUriString();
  40. return parse(restTemplate.getForObject(url, String.class));
  41. }
  42. public JsonNode ccAdvertiserList(String accessToken, long ccAccountId, int page) {
  43. String url = UriComponentsBuilder.fromUriString(CC_ADVERTISER_LIST_URL)
  44. .queryParam("cc_account_id", ccAccountId)
  45. .queryParam("page", page)
  46. .queryParam("page_size", 100)
  47. .queryParam("account_source", "AD")
  48. .build()
  49. .encode()
  50. .toUriString();
  51. HttpHeaders headers = new HttpHeaders();
  52. headers.set("Access-Token", accessToken);
  53. ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(headers), String.class);
  54. return parse(response.getBody());
  55. }
  56. public JsonNode userInfo(String accessToken) {
  57. HttpHeaders headers = new HttpHeaders();
  58. headers.set("Access-Token", accessToken);
  59. ResponseEntity<String> response = restTemplate.exchange(USER_INFO_URL, HttpMethod.GET, new HttpEntity<>(headers), String.class);
  60. return parse(response.getBody());
  61. }
  62. public JsonNode requireSuccess(JsonNode response, String action) {
  63. if (response == null || response.isMissingNode() || response.isNull()) {
  64. throw new BusinessException(500, action + "返回信息为空");
  65. }
  66. int code = response.path("code").asInt(-1);
  67. if (code != 0) {
  68. String message = response.path("message").asText(action + "失败");
  69. throw new BusinessException(500, message);
  70. }
  71. JsonNode data = response.path("data");
  72. if (data.isMissingNode() || data.isNull()) {
  73. throw new BusinessException(500, action + "data信息为空");
  74. }
  75. return data;
  76. }
  77. private JsonNode postJson(String url, Map<String, Object> body) {
  78. HttpHeaders headers = new HttpHeaders();
  79. headers.setContentType(MediaType.APPLICATION_JSON);
  80. return parse(restTemplate.postForObject(url, new HttpEntity<>(body, headers), String.class));
  81. }
  82. private JsonNode parse(String response) {
  83. if (!StringUtils.hasText(response)) {
  84. return objectMapper.missingNode();
  85. }
  86. try {
  87. return objectMapper.readTree(response);
  88. } catch (Exception ex) {
  89. throw new BusinessException(500, "媒体响应解析失败");
  90. }
  91. }
  92. }