获得授权地址
public String getShopeeCode() {
long timest = System.currentTimeMillis() / 1000L;
String tmp_base_string = String.format("%s%s%s", ShopeeAddressConstants.PARTNER_ID, ShopeeAddressConstants.AUTHORIZED_ADDRESS, timest);
byte[] partner_key;
byte[] base_string;
String sign = "";
try {
base_string = tmp_base_string.getBytes("UTF-8");
partner_key = ShopeeAddressConstants.SECRET_KEY.getBytes("UTF-8");
Mac mac = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec(partner_key, "HmacSHA256");
mac.init(secret_key);
sign = String.format("%064x", new BigInteger(1, mac.doFinal(base_string)));
} catch (Exception e) {
e.printStackTrace();
}
String url = ShopeeAddressConstants.HOST.concat(ShopeeAddressConstants.AUTHORIZED_ADDRESS) + String.format("?partner_id=%s×tamp=%s&sign=%s&redirect=%s", ShopeeAddressConstants.PARTNER_ID, timest, sign, ShopeeAddressConstants.REDIRECT_URL);
return url;
}
获得token
public Map<String, Object> getToken(String code, long shopId, Long userId) throws MalformedURLException {
try {
disableSSLCertificateChecking();
} catch (Exception e) {
e.printStackTrace();
}
Map<String, Object> map1 = new HashMap<>();
long timest = System.currentTimeMillis() / 1000L;
String tmp_base_string = String.format("%s%s%s", ShopeeAddressConstants.PARTNER_ID, ShopeeAddressConstants.GET_TOKENS, timest);
byte[] partner_key;
byte[] base_string;
BigInteger sign = null;
String result = "";
try {
base_string = tmp_base_string.getBytes("UTF-8");
partner_key = ShopeeAddressConstants.SECRET_KEY.getBytes("UTF-8");
Mac mac = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec(partner_key, "HmacSHA256");
mac.init(secret_key);
sign = new BigInteger(1, mac.doFinal(base_string));
} catch (Exception e) {
e.printStackTrace();
}
String tmp_url = ShopeeAddressConstants.HOST.concat(ShopeeAddressConstants.GET_TOKENS) + String.format("?partner_id=%s×tamp=%s&sign=%s", ShopeeAddressConstants.PARTNER_ID, timest, String.format("%032x", sign));
System.out.println(tmp_url);
URL url = new URL(tmp_url);
HttpURLConnection conn = null;
PrintWriter out = null;
BufferedReader in = null;
try {
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setConnectTimeout(300000);
conn.setReadTimeout(100000);
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Accept", "application/json");
Map<String, Object> map = new HashMap<>();
map.put("code", code);
map.put("shop_id", shopId);
map.put("partner_id", ShopeeAddressConstants.PARTNER_ID);
String json = JSON.toJSONString(map);
conn.connect();
out = new PrintWriter(conn.getOutputStream());
out.print(json);
out.flush();
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = "";
while ((line = in.readLine()) != null) {
result += line;
}
JSONObject jsonObject = JSONObject.parseObject(result);
map1.put("accessToken", (String) jsonObject.get("access_token"));
map1.put("refreshToken", (String) jsonObject.get("refresh_token"));
map1.put("expireIn", (System.currentTimeMillis() + jsonObject.getLongValue("expire_in")));
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
return map1;
}
刷新token
public Map<String, Object> refreshToken(long shopId) throws MalformedURLException {
try {
disableSSLCertificateChecking();
} catch (Exception e) {
e.printStackTrace();
}
String token = stringRedisTemplate.opsForValue().get(shopId + "");
Map map2 = new Gson().fromJson(token, Map.class);
Object refreshToken = map2.get("refreshToken");
Map<String, Object> map1 = new HashMap<>();
long timest = System.currentTimeMillis() / 1000L;
String tmp_base_string = String.format("%s%s%s", ShopeeAddressConstants.PARTNER_ID, ShopeeAddressConstants.REFRESH_TOKEN, timest);
byte[] partner_key;
byte[] base_string;
BigInteger sign = null;
String result = "";
try {
base_string = tmp_base_string.getBytes("UTF-8");
partner_key = ShopeeAddressConstants.SECRET_KEY.getBytes("UTF-8");
Mac mac = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec(partner_key, "HmacSHA256");
mac.init(secret_key);
sign = new BigInteger(1, mac.doFinal(base_string));
} catch (Exception e) {
e.printStackTrace();
}
String tmp_url = ShopeeAddressConstants.HOST.concat(ShopeeAddressConstants.REFRESH_TOKEN) + String.format("?partner_id=%s×tamp=%s&sign=%s", ShopeeAddressConstants.PARTNER_ID, timest, String.format("%032x", sign));
URL url = new URL(tmp_url);
HttpURLConnection conn = null;
BufferedWriter out = null;
BufferedReader in = null;
try {
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setConnectTimeout(30000);
conn.setReadTimeout(10000);
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Accept", "application/json");
Map<String, Object> map = new HashMap<>();
map.put("refresh_token", refreshToken);
map.put("partner_id", ShopeeAddressConstants.PARTNER_ID);
map.put("shop_id", shopId);
String json = JSON.toJSONString(map);
conn.connect();
out = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream(), "UTF-8"));
out.write(json);
out.flush();
int httpRspCode = conn.getResponseCode();
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = "";
while ((line = in.readLine()) != null) {
result += line;
}
JSONObject jsonObject = JSONObject.parseObject(result);
if (httpRspCode == HttpURLConnection.HTTP_OK && jsonObject.get("error").toString().equals("")) {
map1.put("accessToken", jsonObject.get("access_token"));
map1.put("refreshToken", jsonObject.get("refresh_token"));
map1.put("expireIn", (System.currentTimeMillis() + jsonObject.getLongValue("expire_in")));
stringRedisTemplate.opsForValue().set(shopId + "", new Gson().toJson(map1));
} else {
map1.put("error", jsonObject);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
return map1;
}
取消授权
public Map<String, Object> CancelAuthorization() throws MalformedURLException {
Map<String, Object> map = new HashMap<>();
long timest = System.currentTimeMillis() / 1000L;
String tmp_base_string = String.format("%s%s%s", ShopeeAddressConstants.PARTNER_ID, ShopeeAddressConstants.CANCEL_AUTHORIZATION, timest);
byte[] partner_key;
byte[] base_string;
String sign = "";
try {
base_string = tmp_base_string.getBytes("UTF-8");
partner_key = ShopeeAddressConstants.SECRET_KEY.getBytes("UTF-8");
Mac mac = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec(partner_key, "HmacSHA256");
mac.init(secret_key);
sign = String.format("%064x", new BigInteger(1, mac.doFinal(base_string)));
} catch (Exception e) {
e.printStackTrace();
}
String url = ShopeeAddressConstants.HOST.concat(ShopeeAddressConstants.CANCEL_AUTHORIZATION) + String.format("?partner_id=%s×tamp=%s&sign=%s&redirect=%s", ShopeeAddressConstants.PARTNER_ID, timest, sign, ShopeeAddressConstants.REDIRECT_URL);
map.put("URL", url);
return map;
}
获得商店信息
public Map<String, Object> getStoreInformation(Long shopId) throws MalformedURLException {
try {
disableSSLCertificateChecking();
} catch (Exception e) {
e.printStackTrace();
}
String token = stringRedisTemplate.opsForValue().get(shopId + "");
Map map = new Gson().fromJson(token, Map.class);
Object access_token = map.get("accessToken");
Map<String, Object> map1 = new HashMap<>();
long timest = System.currentTimeMillis() / 1000L;
String tmp_base_string = String.format("%s%s%s%s%s", ShopeeAddressConstants.PARTNER_ID, ShopeeAddressConstants.GET_STORE_INFORMATION, timest, access_token, shopId);
byte[] partner_key;
byte[] base_string;
String result = "";
String sign = "";
try {
base_string = tmp_base_string.getBytes("UTF-8");
partner_key = ShopeeAddressConstants.SECRET_KEY.getBytes("UTF-8");
Mac mac = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec(partner_key, "HmacSHA256");
mac.init(secret_key);
sign = String.format("%064x", new BigInteger(1, mac.doFinal(base_string)));
} catch (Exception e) {
e.printStackTrace();
}
String tmp_url = ShopeeAddressConstants.HOST.concat(ShopeeAddressConstants.GET_STORE_INFORMATION) + String.format("?access_token=%s&partner_id=%s&shop_id=%s&sign=%s×tamp=%s", access_token, ShopeeAddressConstants.PARTNER_ID, shopId, sign, timest);
URL url = new URL(tmp_url);
HttpURLConnection conn = null;
BufferedWriter out = null;
BufferedReader in = null;
try {
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setConnectTimeout(30000);
conn.setReadTimeout(10000);
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Accept", "application/json");
conn.connect();
int httpRspCode = conn.getResponseCode();
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = "";
while ((line = in.readLine()) != null) {
result += line;
}
JSONObject jsonObject = JSONObject.parseObject(result);
map1.put("result", jsonObject);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
return map1;
}
更新商店
public Map<String, Object> updateShop(Long shopId) throws MalformedURLException {
try {
disableSSLCertificateChecking();
} catch (Exception e) {
e.printStackTrace();
}
String token = stringRedisTemplate.opsForValue().get(shopId + "");
Map map = new Gson().fromJson(token, Map.class);
Object access_token = map.get("accessToken");
Map<String, Object> map1 = new HashMap<>();
long timest = System.currentTimeMillis() / 1000L;
String tmp_base_string = String.format("%s%s%s%s%s", ShopeeAddressConstants.PARTNER_ID, ShopeeAddressConstants.UPDATE_SHOP_INFO, timest, access_token, shopId);
byte[] partner_key;
byte[] base_string;
String result = "";
String sign = "";
try {
base_string = tmp_base_string.getBytes("UTF-8");
partner_key = ShopeeAddressConstants.SECRET_KEY.getBytes("UTF-8");
Mac mac = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec(partner_key, "HmacSHA256");
mac.init(secret_key);
sign = String.format("%064x", new BigInteger(1, mac.doFinal(base_string)));
} catch (Exception e) {
e.printStackTrace();
}
String tmp_url = ShopeeAddressConstants.HOST.concat(ShopeeAddressConstants.UPDATE_SHOP_INFO) + String.format("?access_token=%s&partner_id=%s&shop_id=%s&sign=%s×tamp=%s", access_token, ShopeeAddressConstants.PARTNER_ID, shopId, sign, timest);
URL url = new URL(tmp_url);
HttpURLConnection conn = null;
BufferedWriter out = null;
BufferedReader in = null;
try {
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setConnectTimeout(30000);
conn.setReadTimeout(10000);
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Accept", "application/json");
Map<String, Object> map2 = new HashMap<>();
map2.put("shop_name", "hongDou6622");
map2.put("shop_logo", "");//选着对应国家地址
map2.put("description", "test interface");
String json = JSON.toJSONString(map2);
conn.connect();
out = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream(), "UTF-8"));
out.write(json);
out.flush();
int httpRspCode = conn.getResponseCode();
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = "";
while ((line = in.readLine()) != null) {
result += line;
}
JSONObject jsonObject = JSONObject.parseObject(result);
map1.put("Object", jsonObject);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
return map1;
}
获得店铺信息
public Map<String, Object> getInformation(Long shopId) throws MalformedURLException {
try {
disableSSLCertificateChecking();
} catch (Exception e) {
e.printStackTrace();
}
String token = stringRedisTemplate.opsForValue().get(shopId + "");
Map map = new Gson().fromJson(token, Map.class);
Object access_token = map.get("accessToken");
Map<String, Object> map1 = new HashMap<>();
long timest = System.currentTimeMillis() / 1000L;
String tmp_base_string = String.format("%s%s%s%s%s", ShopeeAddressConstants.PARTNER_ID, ShopeeAddressConstants.UPDATE_SHOP_INFO, timest, access_token, shopId);
byte[] partner_key;
byte[] base_string;
String result = "";
String sign = "";
try {
base_string = tmp_base_string.getBytes("UTF-8");
partner_key = ShopeeAddressConstants.SECRET_KEY.getBytes("UTF-8");
Mac mac = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec(partner_key, "HmacSHA256");
mac.init(secret_key);
sign = String.format("%064x", new BigInteger(1, mac.doFinal(base_string)));
} catch (Exception e) {
e.printStackTrace();
}
String tmp_url = ShopeeAddressConstants.HOST.concat(ShopeeAddressConstants.UPDATE_SHOP_INFO) + String.format("?access_token=%s&partner_id=%s&shop_id=%s&sign=%s×tamp=%s", access_token, ShopeeAddressConstants.PARTNER_ID, shopId, sign, timest);
URL url = new URL(tmp_url);
HttpURLConnection conn = null;
BufferedWriter out = null;
BufferedReader in = null;
try {
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setConnectTimeout(30000);
conn.setReadTimeout(10000);
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Accept", "application/json");
conn.connect();
int httpRspCode = conn.getResponseCode();
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = "";
while ((line = in.readLine()) != null) {
result += line;
}
JSONObject jsonObject = JSONObject.parseObject(result);
/* if (httpRspCode == HttpURLConnection.HTTP_OK && jsonObject.get("error").toString().equals("")) {
map1.put("accessToken", jsonObject.get("access_token"));
map1.put("refreshToken", jsonObject.get("refresh_token"));
map1.put("expireIn", jsonObject.get("expire_in"));
} else {*/
map1.put("result", jsonObject);
/* }*/
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
return map1;
}
上传照片
public Map<String, Object> uploadImg(MultipartFile[] files) throws Exception {
MultipartFile multipartFile = files[0];
String fileName = multipartFile.getOriginalFilename();
InputStream inputStream = multipartFile.getInputStream();
byte[] bytes = InputStreamToByte(inputStream);
disableSSLCertificateChecking();
Map<String, Object> map1 = new HashMap<>();
long timest = System.currentTimeMillis() / 1000L;
String tmp_base_string = String.format("%s%s%s", ShopeeAddressConstants.PARTNER_ID, ShopeeAddressConstants.IMAGE_UPLOAD_PATH, timest);
byte[] partner_key;
byte[] base_string;
BigInteger sign = null;
String result = "";
try {
base_string = tmp_base_string.getBytes("UTF-8");
partner_key = ShopeeAddressConstants.SECRET_KEY.getBytes("UTF-8");
Mac mac = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec(partner_key, "HmacSHA256");
mac.init(secret_key);
sign = new BigInteger(1, mac.doFinal(base_string));
} catch (Exception e) {
e.printStackTrace();
}
String tmp_url = ShopeeAddressConstants.HOST.concat(ShopeeAddressConstants.IMAGE_UPLOAD_PATH) + String.format("?partner_id=%s×tamp=%s&sign=%s", ShopeeAddressConstants.PARTNER_ID, timest, String.format("%032x", sign));
System.out.println(tmp_url);
URL url = new URL(tmp_url);
HttpURLConnection conn = null;
DataOutputStream out = null;
BufferedReader in = null;
try {
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setRequestProperty("Charset", "UTF-8");
conn.setRequestProperty("Accept", "application/json");
//conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + "*****");
out = new DataOutputStream(conn.getOutputStream());
out.write(("--" + "*****" + "\r\n").getBytes());
out.write(("Content-Disposition: form-data; name=\"image\"; filename=\"" + fileName + "\"\r\n").getBytes());
out.write(("Content-Type: image/jpeg\r\n").getBytes());
out.write("\r\n".getBytes());
// Write the file content
out.write(bytes);
out.write("\r\n".getBytes());
out.write(("--" + "*****" + "--" + "\r\n").getBytes());
out.flush();
// Close the output stream
//out.close();
int httpRspCode = conn.getResponseCode();
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = "";
while ((line = in.readLine()) != null) {
result += line;
}
JSONObject jsonObject = JSONObject.parseObject(result);
System.out.println(httpRspCode);
map1.put("object", jsonObject);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
return map1;
}
获得订单
public Map<String, Object> getOrderList(Long shopId) throws Exception {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date parse = simpleDateFormat.parse("2024-09-01 00:00:00");
System.out.println(parse.getTime());
long endDate = System.currentTimeMillis();
try {
disableSSLCertificateChecking();
} catch (Exception e) {
e.printStackTrace();
}
String token = stringRedisTemplate.opsForValue().get(shopId + "");
Map map = new Gson().fromJson(token, Map.class);
Object accessToken = map.get("accessToken");
Map<String, Object> map1 = new HashMap<>();
Long timest = System.currentTimeMillis() / 1000L;
String tmp_base_string = String.format("%s%s%s%s%s", ShopeeAddressConstants.PARTNER_ID, ShopeeAddressConstants.GET_ORDER_LIST, timest, accessToken, shopId);
byte[] partner_key;
byte[] base_string;
String result = "";
String sign = "";
try {
base_string = tmp_base_string.getBytes("UTF-8");
partner_key = ShopeeAddressConstants.SECRET_KEY.getBytes("UTF-8");
Mac mac = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec(partner_key, "HmacSHA256");
mac.init(secret_key);
sign = String.format("%064x", new BigInteger(1, mac.doFinal(base_string)));
} catch (Exception e) {
e.printStackTrace();
}
String tmp_url = ShopeeAddressConstants.HOST.concat(ShopeeAddressConstants.GET_ORDER_LIST) + String.format("?partner_id=%s&access_token=%s&sign=%s×tamp=%s&shop_id=%s&time_range_field=%s&time_from=%s&time_to=%s&page_size=%s&response_optional_fields=order_status", ShopeeAddressConstants.PARTNER_ID, accessToken, sign, timest, shopId, "create_time", (parse.getTime() / 1000), (endDate / 1000), 10);
System.out.println(tmp_url);
URL url = new URL(tmp_url);
HttpURLConnection conn = null;
BufferedWriter out = null;
BufferedReader in = null;
try {
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setConnectTimeout(30000);
conn.setReadTimeout(10000);
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Accept", "application/json");
conn.connect();
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = "";
while ((line = in.readLine()) != null) {
result += line;
}
JSONObject jsonObject = JSONObject.parseObject(result);
/* if (httpRspCode == HttpURLConnection.HTTP_OK && jsonObject.get("error").toString().equals("")) {
map1.put("accessToken", jsonObject.get("access_token"));
map1.put("refreshToken", jsonObject.get("refresh_token"));
map1.put("expireIn", jsonObject.get("expire_in"));
} else {*/
map1.put("result", jsonObject);
/* }*/
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
return map1;
}
其他接口类似,参看官网接口文档