约 448 字大约 1 分钟
接入准备
接口授权
希望您的授权标头使用下列格式,记录所需的 PHT 前缀。此前缀可确保为您的请求应用正确的授权规则 Authorization: PHT apikey={{api_key}},signature={{signature}},timestamp={{timestamp}}
说明:
api_key:应用ID shared_secret : 密钥 Authorization:计算逻辑是将 api_key、shared_secret 和当前 Unix 时间戳拼接后通过 SHA-512 加密生成 signature。
⚠️:您的 timestamp 值必须与用于生成 signature 的值****相同。如果提供了不同的时间戳值,纷客行 将无法验证您的签名哈希值,您的请求也将被拒绝
JAVA
public static String getAuthorization(String apiKey, String secret) {
Date date = new Date();
long timestamp = (date.getTime() / 1000);
String signature = null;
try
{
String toBeHashed = apiKey + secret + timestamp;
MessageDigest md = MessageDigest.getInstance("SHA-512");
byte[] bytes = md.digest(toBeHashed.getBytes(StandardCharsets.UTF_8));
StringBuilder sb = new StringBuilder();
for (byte aByte : bytes)
{
sb.append(Integer.toString((aByte & 0xff) + 0x100, 16).substring(config.js));
}
signature = sb.toString();
} catch (NoSuchAlgorithmException e)
{
e.printStackTrace();
}
String authHeaderValue = "PHT apikey=" + apiKey + ",signature=" + signature + ",timestamp=" + timestamp;
return authHeaderValue;
}.NET
public static string getAuthorization(string apiKey, string secret)
{
TimeSpan epochTicks = new TimeSpan(new DateTime(1970, 1, 1).Ticks);
TimeSpan unixTicks = new TimeSpan(DateTime.UtcNow.Ticks) - epochTicks;
double unixTime = (int)unixTicks.TotalSeconds;
var toBeHashed = apiKey + secret + unixTime;
var bytes = System.Text.Encoding.UTF8.GetBytes(toBeHashed);
string authHeaderValue = "";
using (var hash = System.Security.Cryptography.SHA512.Create())
{
var hashedInputBytes = hash.ComputeHash(bytes);
var hashedInputStringBuilder = new System.Text.StringBuilder(128);
foreach (var b in hashedInputBytes)
hashedInputStringBuilder.Append(b.ToString("X2"));
var signature = hashedInputStringBuilder.ToString();
authHeaderValue = "PHT apikey=" + apiKey + ",signature=" + signature + ",timestamp=" + unixTime;
}
return authHeaderValue;
}POSTMAN
// check preconditions
if (!postman.getEnvironmentVariable('api_key') || !postman.getEnvironmentVariable('shared_secret')) {
postman.setNextRequest(null);
console.log('The api_key or shared_secret environment variables have not been set');
// this method is not defined, so will give a clear error to user
// authentication_variables();
}
// setup authorization, checkin and checkout dates
postman.setGlobalVariable('authorization', getAuthorizationKey());
// postman.setGlobalVariable('checkin', getIsoDate(30));
// postman.setGlobalVariable('checkout', getIsoDate(32));
// postman.setGlobalVariable('arrival', getIsoTime(30));
function getAuthorizationKey() {
var timestamp = Math.floor(new Date().getTime()/1000).toString();
return 'PHT apikey=' + postman.getEnvironmentVariable('api_key')
+ ',signature=' + getSigSHA512(timestamp)
+ ',timestamp=' + timestamp;
}
function getSigSHA512(current_timestamp) {
var sig = postman.getEnvironmentVariable('api_key')
+ postman.getEnvironmentVariable('shared_secret') + current_timestamp;
return CryptoJS.SHA512(sig).toString();
}
function getIsoTime(days_from_today) {
var MS_PER_DAY = 24 * 60 * 60 * 1000;
var date = new Date(new Date().getTime() + days_from_today * MS_PER_DAY);
return date.toISOString();
}
function getIsoDate(days_from_today) {
var date = new Date(getIsoTime(days_from_today))
return date.getFullYear() + '-' + pad(date.getMonth()+1) + '-' + pad(date.getDate());
}
function pad(number) {
var r = String(number);
if ( r.length === 1 ) {
r = '0' + r;
}
return r;
}