👏欢迎访问食亨开放平台,这里将提供给你优质的接口,请选择对应系统tab进入到接口文档
import org.apache.commons.codec.binary.Base64;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
/***
@description:*token生成demo
@author: *
@createDate: 2023/4/21*
@version: 1.0
*/
public class TestOauth {
private static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
private static final String DEFAULT_HASH_ALGORITHM = "SHA-1";
/**
* token
*
* @param appId
* @param appKey
* @return
*/
public static String getToken(String appId, String appKey) {
Integer timestamp = getSecondTimestamp();
String str = appId + "," + timestamp + ","
+ signature(appId, appKey, timestamp);
String token = Base64.encodeBase64String(str.getBytes());
return token;
}
/**
* 签名串
*
* @param appId
* @param appKey
* @param timestamp
* @return
*/
public static String signature(String appId, String appKey, Integer timestamp) {
return getDigest(appId + appKey + timestamp,DEFAULT_HASH_ALGORITHM);
}
/**
* 时间戳(精确到秒)
*
* @return
*/
private static Integer getSecondTimestamp() {
String timestamp = String
.valueOf(getSysdate().getTime() / 1000);
return Integer.valueOf(timestamp);
}
public static Date getSysdate() {
return new Date((System.currentTimeMillis() / 1000) * 1000);
}
public static String getDigest(String input, String algorithm) {
String result = "";
try {
MessageDigest md = MessageDigest.getInstance(algorithm);
byte[] src = input.getBytes(DEFAULT_CHARSET);
byte[] digest = md.digest(src);
result = toHexString(digest);
} catch (NoSuchAlgorithmException e) {
result = "";
}
return result;
}
public static String toHexString(byte[] bytes) {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
int val = bytes[i] & 0xFF;
if (val < 0x10) {
builder.append("0");
}
builder.append(Integer.toString(val, 16));
}
return builder.toString();
}
public static void main(String[] args) {
String getToken = getToken("appId", "appKey");
System.out.println(getToken);
}
}