Created
December 4, 2022 18:32
-
-
Save kush-agra/6fe3387035682cc0db9d9e6afb8cfdec to your computer and use it in GitHub Desktop.
Order things from amazon
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Import the necessary modules | |
import requests | |
import json | |
# Set your Amazon credentials | |
AMAZON_ACCESS_KEY = 'YOUR_ACCESS_KEY' | |
AMAZON_SECRET_KEY = 'YOUR_SECRET_KEY' | |
AMAZON_ASSOCIATE_TAG = 'YOUR_ASSOCIATE_TAG' | |
# Set the item you want to order | |
item = 'B07H3QM2Q8' | |
# Set the endpoint and required parameters for the API request | |
endpoint = 'https://webservices.amazon.com/onca/xml' | |
params = { | |
'Service': 'AWSECommerceService', | |
'Operation': 'CartCreate', | |
'AWSAccessKeyId': AMAZON_ACCESS_KEY, | |
'AssociateTag': AMAZON_ASSOCIATE_TAG, | |
'Item.1.ASIN': item, | |
'Item.1.Quantity': 1 | |
} | |
# Send the API request and get the response | |
response = requests.get(endpoint, params=params) | |
# Parse the XML response into a dictionary | |
data = xmltodict.parse(response.text) | |
# Check if the request was successful | |
if data['CartCreateResponse']['Cart']['Request']['IsValid'] == 'True': | |
print('Success! Your cart was created.') | |
else: | |
print('Sorry, there was an error creating your cart.') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment