Hypixel的API永久用密钥请求

主要是获取永久密钥【72小时自动更新Hypixel密钥】

为确保API调用的清晰理解与正确实施,以下是对你的API服务的详细调用文档。这份文档将包含关键信息,如API的功能描述、请求的URL、必要的参数、示例请求、响应格式和常见错误处理。

API功能描述

这个API主要的功能就是自动获取Hypixel的API密钥,此API密钥为永久API密钥,但是该API密钥不会提供明文服务,所有Hypixel的API密钥72个小时之后都会替换成新的,只能通过这个想要获取必须要获取授权码才可以获取访问,可以免于一次次输入Hypixel的API密钥信息

基本信息

请求参数

参数名 必须 类型 描述
key string 已注册的访问授权码
timestamp int 请求的时间戳
signature string 根据密钥和时间戳生成的签名

生成签名

请求签名应该使用以下格式生成:

signature = HMAC_SHA256("key=YOUR_KEY&timestamp=YOUR_TIMESTAMP", "{server_verification_public_key}")

这里的 YOUR_KEY 是请求使用的已注册的访问授权码,YOUR_TIMESTAMP 是请求的时间戳,server_verification_public_key 是服务器与客户端之间的生成签名的秘钥。

示例请求

以下是一个使用CURL工具的API调用示例:

curl -X GET "https://api.tianbeigm.cn/api_key.php?key=4fc6df45e11759b49712209cfd0e573b1&timestamp=1609459200&signature=generated_signature"

响应格式

成功的响应将以JSON格式返回,包含API密钥:

{
  "api_key": "hypixel的api_key"
}

失败的响应同样返回JSON格式,描述错误信息:

{
  "error": "拒绝访问:签名不匹配或时间戳过期"
}

请求代码

JavaScript请求
const apiKey = '4fc6df45e11759b49712209cfd0e573b1';
const secretKey = 'generated_signature'; // 生成签名的秘钥(列示秘钥)

async function generateSignature(apiKey, timestamp) {
    const encoder = new TextEncoder();
    const keyData = encoder.encode(secretKey);
    const key = await crypto.subtle.importKey('raw', keyData, { name: 'HMAC', hash: { name: 'SHA-256' }}, true, ['sign']);
    const message = `key=${apiKey}&timestamp=${timestamp}`;
    const signature = await crypto.subtle.sign('HMAC', key, encoder.encode(message));
    return Array.from(new Uint8Array(signature)).map(b => b.toString(16).padStart(2, '0')).join('');
}

async function fetchApiKey() {
    const timestamp = Math.floor(Date.now() / 1000); // 生成时间戳
    const signature = await generateSignature(apiKey, timestamp); // 生成签名

    const url = `https://api.tianbeigm.cn/api_key.php?key=${apiKey}&timestamp=${timestamp}&signature=${signature}`;

    fetch(url)
    .then(response => response.json())
    .then(data => {
        if (data.api_key) {
            document.getElementById('api_key').innerText = 'API Key: ' + data.api_key;
        } else {
            document.getElementById('api_key').innerText = 'Error: ' + data.error;
        }
    })
    .catch(error => {
        console.error('Error fetching the API key:', error);
        document.getElementById('api_key').innerText = 'Network error or invalid URL';
    });
}

window.onload = fetchApiKey;
PHP请求
<?php

$apiKey = '4fc6df45e11759b49712209cfd0e573b1';
$secretKey = 'generated_signature'; // 生成签名的秘钥(列示秘钥)

function generateSignature($apiKey, $timestamp) {
    $message = "key={$apiKey}&timestamp={$timestamp}";
    return hash_hmac('sha256', $message, $GLOBALS['secretKey']);
}

function fetchApiKey() {
    $timestamp = time(); // 生成时间戳
    $signature = generateSignature($GLOBALS['apiKey'], $timestamp); // 生成签名

    $url = "https://api.tianbeigm.cn/api_key.php?key={$GLOBALS['apiKey']}&timestamp={$timestamp}&signature={$signature}";

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    $data = json_decode($response, true);
    curl_close($ch);

    if (isset($data['api_key'])) {
        echo 'API Key: ' . $data['api_key'];
    } else {
        echo 'Error: ' . $data['error'];
    }
}

fetchApiKey(); // 该函数在文件载入时执行
?>
Python请求
import hashlib
import hmac
import time
import requests

api_key = '4fc6df45e11759b49712209cfd0e573b1'
secret_key = 'generated_signature'  # 生成签名的秘钥

def generate_signature(api_key, timestamp):
    message = f"key={api_key}&timestamp={timestamp}".encode('utf-8')
    signature = hmac.new(secret_key.encode('utf-8'), message, hashlib.sha256).hexdigest()
    return signature

def fetch_api_key():
    timestamp = int(time.time())  # 生成时间戳
    signature = generate_signature(api_key, timestamp)  # 生成签名

    url = f"https://api.tianbeigm.cn/api_key.php?key={api_key}&timestamp={timestamp}&signature={signature}"
    
    response = requests.get(url)
    data = response.json()

    if 'api_key' in data:
        print('API Key:', data['api_key'])
    else:
        print('Error:', data.get('error', 'Unknown error'))

fetch_api_key()  # 调用函数获取 API Key

Java请求
public class ApiKeyFetcher {

    private static final String API_KEY = "4fc6df45e11759b49712209cfd0e573b1";
    private static final String SECRET_KEY = "generated_signature"; // 生成签名的秘钥

    public static void main(String[] args) {
        try {
            fetchApiKey();
        } catch (Exception e) {
            System.err.println("Error fetching the API key: " + e.getMessage());
        }
    }

    private static void fetchApiKey() throws Exception {
        long timestamp = Instant.now().getEpochSecond();
        String signature = generateSignature(API_KEY, timestamp);

        String url = String.format("https://api.tianbeigm.cn/api_key.php?key=%s&timestamp=%d&signature=%s", 
                                    API_KEY, timestamp, signature);

        try (CloseableHttpClient client = HttpClients.createDefault()) {
            HttpGet request = new HttpGet(url);
            String response = EntityUtils.toString(client.execute(request).getEntity());
            System.out.println("API Key: " + response); // 响应处理
        }
    }

    private static String generateSignature(String apiKey, long timestamp) throws Exception {
        String message = String.format("key=%s&timestamp=%d", apiKey, timestamp);
        byte[] keyData = SECRET_KEY.getBytes(StandardCharsets.UTF_8);
        SecretKeySpec keySpec = new SecretKeySpec(keyData, "HmacSHA256");

        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(keySpec);
        byte[] result = mac.doFinal(message.getBytes(StandardCharsets.UTF_8));
        return Hex.encodeHexString(result);
    }
}

错误处理

常见的错误及其描述如下:

  • 拒绝访问:未提供完整的验证信息

    • 当请求缺少 key, timestampsignature 参数时返回。
  • 拒绝访问:签名不匹配或时间戳过期

    • 当提供的签名不正确或时间戳不在允许的时间范围内(默认5分钟)时返回。
  • 拒绝访问:您的验证信息错误或者未被授权

    • 当提供的已注册的访问授权码不在允许的列表中时返回。
  • Error: API key not found.

    • 当无法在服务器上找到对应的Hypixel的API密钥时返回。

安全提示

  • 使用HTTPS协议来保护您的请求数据免遭窃听。
  • 保护您的API密钥和共享秘密不被泄露。
  • 确保时间戳的精确性以避免因时差导致的认证失败。

获取访问授权码和生成签名的秘钥

访问授权码以及生成签名的秘钥获取并通过以下方式进行联系

  • 请联系QQ:2129373966
  • 联系邮箱:numakkiyu@zyghit.cn