Forum Discussion
Are you using LMv1 auth? Are you need to include the post data into the signature header hash?
Thanks Mike for your reply.
Below is the code i am trying to execute.
def generate_signature(http_verb, resource_path, request_body=""):
timestamp = str(int(time.time() * 1000)) # Current time in milliseconds
request_vars = http_verb + timestamp + request_body + resource_path
hmac_digest = hmac.new(ACCESS_KEY.encode(), msg=request_vars.encode(), digestmod=hashlib.sha256).digest()
signature = base64.b64encode(hmac_digest).decode()
return signature, timestamp
# Function to get device ID by hostname
def get_device_id(hostname):
resource_path = f"/device/devices?filter=displayName:{hostname}"
url = BASE_URL + resource_path
signature, timestamp = generate_signature("GET", resource_path)
headers = {
"Authorization": f"LMv1 {ACCESS_ID}:{signature}:{timestamp}",
"Content-Type": "application/json"
}
print("\nš Debugging Authentication:")
print(f"š¹ Access ID: {ACCESS_ID}")
print(f"š¹ Generated Signature: {signature}")
response = requests.get(url, headers=headers, verify=False) # Ignore SSL warning
# š¹ Debugging: Print the response before parsing
print(f"\nš Debugging API Response for {hostname}:")
print(f"š¹ Status Code: {response.status_code}")
print(f"š¹ Response Text: {response.text}")
print(f"š¹ Timestamp: {timestamp}")
# š¹ Check if API returned success before parsing JSON
if response.status_code == 200:
try:
data = response.json()
if data is None:
print(f"ā API returned None for {hostname}. Possible issue with API.")
return None
devices = data.get("data", {}).get("items", [])
return devices[0]["id"] if devices else None
except json.JSONDecodeError:
print(f"ā Failed to decode JSON for {hostname}: {response.text}")
return None
else:
print(f"ā API Error ({response.status_code}): {response.text}")
return None