import base64 import os import sys import requests def encrypt_password(password: str, key_word: str) -> str: from Crypto.Cipher import AES key = key_word.encode('utf-8') iv = key cypher = AES.new(key, AES.MODE_CFB, iv=iv, segment_size=128) ciphertext = cypher.encrypt(password.encode('utf-8')) return base64.b64encode(ciphertext).decode('utf-8') def build_basic_auth(client: str) -> str: b64 = base64.b64encode(client.encode('utf-8')).decode('utf-8') return f'Basic {b64}' def get_token(base_url: str, username: str, password: str) -> dict: enc_key = 'hongxyundapp2025' oauth_client = 'hongxapp:hongxplapp' enc_password = encrypt_password(password, enc_key) url = f"{base_url.rstrip('/')}/auth/oauth2/token" params = { 'username': username, 'randomStr': 'clickWord', 'code': '', 'grant_type': 'password', 'scope': 'server', } data = { 'password': enc_password, } resp = requests.post( url, params=params, data=data, headers={ 'Authorization': build_basic_auth(oauth_client), 'Content-Type': 'application/x-www-form-urlencoded', 'Enc-Flag': 'false', }, timeout=30, ) resp.raise_for_status() return resp.json() def main() -> int: base_url ='http://192.168.0.209:9999' username ='admin' password ='123456' try: res = get_token(base_url, username, password) print(res) access_token = res.get('access_token') if access_token: print('\nACCESS_TOKEN=') print(access_token) return 0 except Exception as e: print(str(e), file=sys.stderr) return 1 if __name__ == '__main__': raise SystemExit(main())