Commit 7d8ca09c authored by Imam M. Jah's avatar Imam M. Jah

added urlof images

parent ce9d5676
......@@ -2,8 +2,8 @@ import requests
from requests.auth import HTTPBasicAuth
# Replace these with your actual Kroger API credentials
CLIENT_ID = 'your_client_id'
CLIENT_SECRET = 'your_client_secret'
CLIENT_ID = 'bagandbyte-2432612430342431786a6a7a4144667543456c792f4433472f6f4f5965785957433762597543636d5752524f6b5554766233664f67703648413533654135037845033016591'
CLIENT_SECRET = 'Yb24nJQRFG8yS73ivzSwTES9cBTfGe39UsjHgEAQ'
# OAuth 2.0 token URL and Kroger API product search URL
TOKEN_URL = 'https://api.kroger.com/v1/connect/oauth2/token'
......@@ -39,7 +39,7 @@ def search_products(token, query):
params = {
'filter.term': query, # Search term, e.g., "milk"
'filter.locationId': '01234567' # Replace with a valid location ID
'filter.locationId': '01400943' # Replace with a valid location ID
}
# Send the GET request to search for products
......@@ -51,6 +51,30 @@ def search_products(token, query):
print(f"Failed to fetch products: {response.status_code}, {response.text}")
return None
def display_products_with_images(products):
for product in products['data']:
product_id = product['productId']
name = product['description']
# Check for images and get the first available image (front perspective)
image_url = None
if 'images' in product and len(product['images']) > 0:
for image in product['images']:
if image['perspective'] == 'front': # Use front-facing image
if len(image['sizes']) > 0:
image_url = image['sizes'][0]['url'] # Fetch the URL of the first size available
break
print(f"Product ID: {product_id}")
print(f"Name: {name}")
if image_url:
print(f"Image URL: {image_url}")
else:
print("No image available.")
print("-" * 40)
# Main code to test the API
if __name__ == "__main__":
# Step 1: Get Access Token
......@@ -61,9 +85,7 @@ if __name__ == "__main__":
# Step 2: Search for products (e.g., search for "milk")
products = search_products(token, 'milk')
if products:
print("Products found:")
for product in products['data']:
print(f"Product ID: {product['productId']}, Description: {product['description']}")
display_products_with_images(products)
else:
print("No products found or an error occurred.")
else:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment