|
@@ -1,10 +1,19 @@
|
|
|
package com.raycos.raycosdatacenterapi.authorize.impl;
|
|
|
|
|
|
+
|
|
|
+import com.alibaba.fastjson2.JSONObject;
|
|
|
import com.raycos.raycoscommon.Internal.datacenterapi.TbAmzAuthorize;
|
|
|
+import com.raycos.raycoscommon.Internal.datacenterapi.TbAmzToken;
|
|
|
import com.raycos.raycosdatacenterapi.authorize.AmzAuthorizeService;
|
|
|
+import com.raycos.raycosdatacenterapi.authorize.AmzTokenService;
|
|
|
import com.raycos.raycosdatacenterapi.mapper.TbAmzAuthorizeMapper;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.http.HttpEntity;
|
|
|
+import org.springframework.http.HttpHeaders;
|
|
|
+import org.springframework.http.MediaType;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.util.Assert;
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
import java.util.List;
|
|
@@ -15,29 +24,36 @@ import java.util.List;
|
|
|
*/
|
|
|
|
|
|
@Service
|
|
|
+@Slf4j
|
|
|
public class AmzAuthorizeServiceImpl implements AmzAuthorizeService {
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
private TbAmzAuthorizeMapper tbAmzAuthorizeMapper;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private AmzTokenService amzTokenService;
|
|
|
+
|
|
|
@Autowired
|
|
|
private RestTemplate restTemplate;
|
|
|
|
|
|
@Override
|
|
|
public List<TbAmzAuthorize> selectAll() throws Exception {
|
|
|
-
|
|
|
-
|
|
|
List<TbAmzAuthorize> list = tbAmzAuthorizeMapper.selectList(null);
|
|
|
|
|
|
return list;
|
|
|
-
|
|
|
-
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void addAmzAuthorize(TbAmzAuthorize tbAmzAuthorize) throws Exception {
|
|
|
+ // 亚马逊 回调接口 返回 LWA code
|
|
|
tbAmzAuthorizeMapper.insert(tbAmzAuthorize);
|
|
|
+ try {
|
|
|
+ // 使用LWA code 获取长短令牌
|
|
|
+ getAccessToken(tbAmzAuthorize);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error(" 亚马逊授权 LWACODE 刷新令牌失败 "+e.getMessage(), e);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -49,9 +65,40 @@ public class AmzAuthorizeServiceImpl implements AmzAuthorizeService {
|
|
|
@Override
|
|
|
public void getAccessToken(TbAmzAuthorize tbAmzAuthorize) throws Exception {
|
|
|
|
|
|
- //
|
|
|
|
|
|
-// restTemplate.
|
|
|
+ RestTemplate restTemplate = new RestTemplate();
|
|
|
+ String url = "https://api.amazon.com/auth/o2/token";
|
|
|
+
|
|
|
+ // 设置请求头
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
+
|
|
|
+ JSONObject params = new JSONObject();
|
|
|
+ params.put("grant_type", "authorization_code");
|
|
|
+ params.put("code", tbAmzAuthorize.getSpapiOauthCode());
|
|
|
+ params.put("redirect_uri", "https://apisaas.raycos.net/erp/amz/authcode");
|
|
|
+ params.put("client_id", "amzn1.application-oa2-client.2d001c4aeaaf438f98ee0b07070c1cf8");
|
|
|
+ params.put("client_secret", "amzn1.oa2-cs.v1.52b13f4ce04e83046b3426b620b6bfb94fd3ce61c815c6f68858af69d0ccbe54");
|
|
|
+
|
|
|
+ // 设置请求体
|
|
|
+ String requestBody = params.toString();
|
|
|
+ // 创建请求实体
|
|
|
+ HttpEntity<String> request = new HttpEntity<>(requestBody, headers);
|
|
|
+ // 发送 POST 请求
|
|
|
+ String response = restTemplate.postForObject(url, request, String.class);
|
|
|
+
|
|
|
+ log.info("Response: " + response);
|
|
|
+
|
|
|
+ try {
|
|
|
+ JSONObject json = JSONObject.parseObject(response);
|
|
|
+ Assert.notNull(json.get("access_token"), " 获取令牌接口返回异常 请查看返回分析 "+json);
|
|
|
+ TbAmzToken tbAmzToken = JSONObject.parseObject(response, TbAmzToken.class);
|
|
|
+ amzTokenService.addAmzToken(tbAmzToken);
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
+ throw e;
|
|
|
+ }
|
|
|
|
|
|
|
|
|
}
|