From feedb51bb36305d3db73fbc80de1261347187b92 Mon Sep 17 00:00:00 2001 From: ronknight Date: Thu, 22 Aug 2024 17:26:37 +0000 Subject: [PATCH] update api calls --- 2createtoken.py | 12 +++++++++--- productlist.py | 16 +++++++++++----- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/2createtoken.py b/2createtoken.py index 24b88ac..828fa46 100644 --- a/2createtoken.py +++ b/2createtoken.py @@ -57,9 +57,15 @@ def generate_signature(params, secret_key, api_operation): print("Formatted Output:") print(formatted_output) +# Add a custom header to mimic the Protocol.GOP behavior +headers = { + 'X-Protocol': 'GOP', # Custom header to indicate the protocol + 'Content-Type': 'application/x-www-form-urlencoded' # Adjust as per API requirements +} + try: - # Make the POST request - response = requests.post(f"{ALIBABA_SERVER_CALL_ENTRY}{API_OPERATION}", data=params) + # Make the POST request with the custom headers + response = requests.post(f"{ALIBABA_SERVER_CALL_ENTRY}{API_OPERATION}", headers=headers, data=params) # Handle the response if response.status_code == 200: @@ -103,4 +109,4 @@ def generate_signature(params, secret_key, api_operation): print("Response:", response.text) except requests.exceptions.RequestException as e: - print(f"\nRequest error: {e}") + print(f"\nRequest error: {e}") \ No newline at end of file diff --git a/productlist.py b/productlist.py index d6a029f..f2d97cf 100644 --- a/productlist.py +++ b/productlist.py @@ -32,10 +32,10 @@ def main(): APP_SECRET = os.getenv('APP_SECRET') ACCESS_TOKEN = os.getenv('ACCESS_TOKEN') ALIBABA_SERVER_CALL_ENTRY = "https://openapi-api.alibaba.com/rest" - API_OPERATION = "/alibaba/icbu/product/list" # Alibaba API operation endpoint + API_OPERATION = "/alibaba/icbu/product/list" # API operation endpoint for GOP protocol # Prepare the request parameters - timestamp = str(int(time.time() * 1000)) # Replace with your actual timestamp logic if needed + timestamp = str(int(time.time() * 1000)) # Current timestamp in milliseconds params = { "app_key": APP_KEY, "format": "json", @@ -51,6 +51,12 @@ def main(): # Add the generated signature to the params dictionary params['sign'] = signature + # Define the headers + headers = { + 'X-Protocol': 'GOP', # Use Protocol.GOP + 'Content-Type': 'application/x-www-form-urlencoded' + } + # Prepare logging log_dir = 'api_logs' os.makedirs(log_dir, exist_ok=True) @@ -60,8 +66,9 @@ def main(): # Prepare request log (excluding sensitive info) request_log = { "Request Time": datetime.now().strftime("%Y-%m-%d %H:%M:%S"), - "Request URL": f"{ALIBABA_SERVER_CALL_ENTRY}{API_OPERATION}", + "Request URL": f"{ALIBABA_SERVER_CALL_ENTRY}", "Request Method": "POST", + "Request Headers": headers, "Request Parameters": { "format": params.get("format"), "method": params.get("method"), @@ -72,7 +79,7 @@ def main(): try: # Make the POST request - response = requests.post(f"{ALIBABA_SERVER_CALL_ENTRY}{API_OPERATION}", data=params) + response = requests.post(f"{ALIBABA_SERVER_CALL_ENTRY}{API_OPERATION}", data=params, headers=headers) # Handle the response response_data = response.json() @@ -102,4 +109,3 @@ def main(): if __name__ == "__main__": main() -