认证请求调用方式

认证请求与普通请求的区别:


你可以从开发者那获取令牌(TOKEN)与密钥(SECRET_KEY),接口提供签名验证和IP白名单验证,为确保请求安全,请您务必启用一种验证方式,若同时启用签名验证和IP验证,您可以从这两种验证方式中任选一种进行请求。


签名验证示例:

python
import time
import hashlib
import hmac
import requests
import uuid
from requests.exceptions import RequestException

def get_mcping(token: str, secret_key: str):
    # 生成当前Unix时间戳(秒级)
    timestamp = str(int(time.time()))
    # 生成唯一随机字符串(每次请求随机生成,2分钟内避免出现重复)
    nonce = uuid.uuid4().hex
    # 计算HMAC-SHA256签名
    signature = hmac.new(
        secret_key.encode(),
        f"{token}{timestamp}{nonce}".encode(),
        hashlib.sha256
    ).hexdigest()
    # 发送请求
    try:
        response = requests.get(
            "https://api.azsu.top/mcping/json",
            params={
                "ip": "azsu.top", # 其他参数
                "token": token, # 您的token
                "timestamp": timestamp, # 时间戳
                "nonce": nonce, # 一次性随机字符串
                "sign": signature # 签名
            },
            timeout=10
        )
        return response.json()
    except RequestException as e:
        print(f"请求失败: {e}")
        return None
        
if __name__ == "__main__":
    TOKEN = "替换为您的token"
    SECRET_KEY = "替换为您的密钥"
    
    result = get_mcping(TOKEN, SECRET_KEY)
    print(result)


API文档