Skip to content

Commit

Permalink
update api calls
Browse files Browse the repository at this point in the history
  • Loading branch information
ronknight committed Aug 22, 2024
1 parent 5066ecd commit feedb51
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
12 changes: 9 additions & 3 deletions 2createtoken.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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}")
16 changes: 11 additions & 5 deletions productlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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)
Expand All @@ -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"),
Expand All @@ -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()
Expand Down Expand Up @@ -102,4 +109,3 @@ def main():

if __name__ == "__main__":
main()

0 comments on commit feedb51

Please sign in to comment.