直接拿到demo运行,其他的自己封装即可
聚水潭 erp文档 聚水潭接入
token 建议放到redis 加过期时间,通过过期时间来判断是否需要刷新token,来重新续命
import okhttp3.*;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.*;
import java.util.Map.Entry;
import static com.taobao.api.internal.util.WebV2Utils.buildQuery;
/** * 〈功能描述〉<br> * ----- : 聚水潭 * * @author xsc * @date 2022/11/11 17:14 */
public class JstTest {
private static final String SIGN_METHOD_MD5 = "md5";
// TOP服务地址,正式环境需要设置为https://openapi.jushuitan.com
private static final String url = "/open/shops/query";
private static final String serverUrl = "https://dev-api.jushuitan.com";
private static final String appKey = "b0b7d1db226d4216a3d58df9ffa2dde5"; // 可替换为您的应用的appKey
private static final String appSecret = "99c4cef262f34ca882975a7064de0b87"; // 可替换为您的应用的appSecret
private static final String accessToken = "b7e3b1e24e174593af8ca5c397e53dad"; // 必须替换为授权得到的真实有效accessToken
private static final MediaType MEDIA_TYPE_TEXT = MediaType.parse("application/x-www-form-urlencoded; charset=utf-8");
/** * 生成新sign https://openweb.jushuitan.com/doc?docId=40 * * @param str * @return */
public static String createSign(String str) {
if (str == null || str.length() == 0) {
return null;
}
char[] hexDigits = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
try {
MessageDigest mdTemp = MessageDigest.getInstance(SIGN_METHOD_MD5);
mdTemp.update(str.getBytes(StandardCharsets.UTF_8));
byte[] md = mdTemp.digest();
int j = md.length;
char[] buf = new char[j * 2];
int k = 0;
int i = 0;
while (i < j) {
byte byte0 = md[i];
buf[k++] = hexDigits[byte0 >>> 4 & 0xf];
buf[k++] = hexDigits[byte0 & 0xf];
i++;
}
return new String(buf);
} catch (Exception e) {
return null;
}
}
// 签名
public static String signTopRequest(Map<String, String> params, String secret, String signMethod) {
// 第一步:检查参数是否已经排序
String[] keys = params.keySet().toArray(new String[0]);
Arrays.sort(keys);
// 第二步:把所有参数名和参数值串在一起
StringBuilder query = new StringBuilder();
if (SIGN_METHOD_MD5.equals(signMethod)) {
query.append(secret);
}
for (String key : keys) {
String value = params.get(key);
if (isNotEmpty(key) && isNotEmpty(value)) {
query.append(key).append(value);
}
}
return createSign(query.toString());
}
/** * 店铺授权聚水潭的URL * * @return */
public static String spliceAuthorizationUrl(String state) throws IOException {
Map<String, String> params = new HashMap<>();
// 公共参数
params.put("app_key", appKey);
params.put("timestamp", String.valueOf(System.currentTimeMillis() / 1000));
params.put("state", state);
params.put("charset", "utf-8");
// 签名参数
params.put("sign", signTopRequest(params, appSecret, SIGN_METHOD_MD5));
String url = buildQuery(params, "utf-8");
url = serverUrl + "/auth?" + url;
return url;
}
/** * 验证签名 * * @param params * @param sign * @return * @throws IOException */
public static Boolean signatureVerification(Map<String, String> params, String sign) throws IOException {
String signTopRequest = signTopRequest(params, appSecret, SIGN_METHOD_MD5);
return sign.equals(signTopRequest);
}
/** * 获取accessToken' * * @param code * @return */
public static String getAccessToken(String code) {
Map<String, String> params = new HashMap<>();
params.put("app_key", appKey);
params.put("timestamp", String.valueOf(System.currentTimeMillis() / 1000));
params.put("grant_type", "authorization_code");
params.put("charset", "utf-8");
params.put("code", code);
params.put("sign", signTopRequest(params, appSecret, SIGN_METHOD_MD5));
//获取accessToken的地址
String url = serverUrl + "/openWeb/auth/accessToken";
return sendByPostMap(url, params);
}
/** * 刷新AccessToken * * @param refreshToken * @return */
public static String refreshToken(String refreshToken) {
Map<String, String> params = new HashMap<>();
params.put("app_key", appKey);
params.put("timestamp", String.valueOf(System.currentTimeMillis() / 1000));
params.put("grant_type", "refresh_token");
params.put("charset", "utf-8");
params.put("refresh_token", refreshToken);
params.put("scope", "all");
params.put("sign", signTopRequest(params, appSecret, SIGN_METHOD_MD5));
//获取accessToken的地址
String url = serverUrl + "/openWeb/auth/refreshToken";
return sendByPostMap(url, params);
}
/** * post方式请求 * <p> * 对于POST请求,请求参数全部放在Body里,不要把参数放在Query里。 -- 聚水潭 * * @param url * @param params * @return */
public static String sendByPostMap(String url, Map<String, String> params) {
OkHttpClient client = new OkHttpClient();
StringBuilder content = new StringBuilder();
// 拼接请求参数
Iterator<Entry<String, String>> iterator = params.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, String> entry = iterator.next();
content.append(entry.getKey()).append("=").append(entry.getValue());
if (iterator.hasNext()) {
content.append("&");
}
}
RequestBody requestBody = RequestBody.create(MEDIA_TYPE_TEXT, content.toString());
Request request = new Request.Builder().url(url).post(requestBody).build();
Response response;
try {
response = client.newCall(request).execute();
assert response.body() != null;
return response.body().string();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/** * 发送请求 * * @param accessToken * @param biz * @param requestUrl * @return */
public static String sendRequest(String accessToken, String biz, String requestUrl) {
// 公共请求参数
Map<String, String> params = new HashMap<>();
params.put("app_key", appKey);
params.put("access_token", "b7e3b1e24e174593af8ca5c397e53dad");
params.put("timestamp", String.valueOf(System.currentTimeMillis() / 1000));
params.put("version", "2");
params.put("charset", "utf-8");
// 业务参数
params.put("biz", biz);
// 签名参数
params.put("sign", signTopRequest(params, appSecret, SIGN_METHOD_MD5));
String url = serverUrl + requestUrl;
return sendByPostMap(url, params);
}
public static boolean isNotEmpty(String value) {
int strLen;
if (value == null || (strLen = value.length()) == 0) {
return false;
}
for (int i = 0; i < strLen; i++) {
if ((!Character.isWhitespace(value.charAt(i)))) {
return true;
}
}
return false;
}
public static void main(String[] args) throws IOException {
sendRequest(accessToken, "{\"page_index\":\"1\",\"page_size\":\"100\",\"nicks\":[\"老板\"]}", url);
}
}
/** * post方式请求 备用 * * @param url * @param params * @return */
public static String sendByPostMap(String url, Map<String, String> params, Map<String, String> map) {
String result;
OkHttpClient client = new OkHttpClient();
StringBuilder content = new StringBuilder();
// 拼接请求参数
Iterator<Map.Entry<String, String>> iterator = params.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, String> entry = iterator.next();
content.append(entry.getKey()).append("=").append(entry.getValue());
if (iterator.hasNext()) {
content.append("&");
}
}
// 请求体
RequestBody requestBody = RequestBody.create(MEDIA_TYPE_TEXT, content.toString());
// 请求头 添加具有名称和值的标头。对于像“Cookie”这样的多值头文件,最好使用这种方法。
Request.Builder builder = new Request.Builder().url(url);
if (MapUtils.isNotEmpty(map)) {
map.forEach(builder::addHeader);
}
Request request = builder.url(url).post(requestBody).build();
Response response;
try {
response = client.newCall(request).execute();
assert response.body() != null;
result = response.body().string();
System.out.println("result = " + result);
return result;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
今天的文章聚水潭对接平台_聚水潭打单使用教程分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/69084.html