const crypto = require('crypto'); const axios = require('axios'); const querystring = require('querystring'); async function makeApiRequest() { const method = 'GET'; // Request method: GET, POST const secretType = 'MD5'; // Verification method: MD5, Hash const apiUrl = 'https://登录后显示/api/22/22'; const appid = '应用id'; // Get from your application settings in Wapi.cn const secret = '应用密钥'; // Get from your application settings in Wapi.cn // Prepare request data const data = { appid: "1", format: "json", time: "1545829466" }; data.appid = appid data.time = Math.floor(Date.now() / 1000) // Generate signature based on secret type if (secretType === 'MD5') { // Sort keys alphabetically const sortedKeys = Object.keys(data).sort(); // Build MD5 string let md5String = ''; sortedKeys.forEach(key => { if (data[key] && data[key].length > 0) { // Skip empty values md5String += key + data[key]; } }); md5String += secret; // Calculate MD5 hash const hash = crypto.createHash('md5').update(md5String).digest('hex'); data.sign = hash; } else if (secretType === 'HASH') { data.sign = secret; } let response; try { if (method === 'GET') { // Make GET request with query parameters const queryString = querystring.stringify(data); const fullUrl = `${apiUrl}?${queryString}`; response = await axios.get(fullUrl); } else { // Make POST request with form data const formData = querystring.stringify(data); response = await axios.post(apiUrl, formData, { headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' } }); } // Print the response data console.log(response.data); return response.data; } catch (error) { console.error('Error making API request:', error.message); if (error.response) { console.error('Response data:', error.response.data); console.error('Response status:', error.response.status); } throw error; } } // Execute the function makeApiRequest() .then(data => console.log('API call successful')) .catch(err => console.error('API call failed'));