Define the API endpoints
login_url = 'https://smartapi.angelbroking.com/rest/auth/login'
market_data_url = 'https://smartapi.angelbroking.com/rest/secure/angelbroking/currentdayextremes'
Prepare the request headers for authentication
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
}
Prepare the request data for authentication
data = {
'api_key': api_key,
'password': api_secret,
}
Authenticate and obtain an access token
response = requests.post(login_url, data=data, headers=headers)
if response.status_code == 200:
access_token = response.json()['data']['accessToken']
# Prepare headers for fetching market data with the access token
headers = {
'Authorization': f'Bearer {access_token}',
'Content-Type': 'application/json',
}
# Fetch today's Bank Nifty price
response = requests.get(market_data_url, headers=headers)
if response.status_code == 200:
try:
market_data = response.json()
today_banknifty_price = market_data['data']['CASH']['BANKNIFTY']
print(f"Today's Bank Nifty Price: {today_banknifty_price}")
except json.JSONDecodeError:
print("Error: Unable to decode JSON response.")
else:
print(f"Error fetching market data: {response.status_code} - {response.text}")
else:
print(f"Authentication error: {response.status_code} - {response.text}")
kindly please guide where I am doing mistake.