import streamlit as st import requests import pandas as pd # ---------------------- CONFIG ---------------------- CLIENT_ID = "3MVG9VMBZCsTL9hnfx2eVMOHa56mwSZnvuAnPr3kVVBEQfeLYvrSfJNRRjjSlKWPLy99XM6kefg==" CLIENT_SECRET = "6F7E9C95CE20CC07FC1EBD39B34909739D99975A0EEB548240926EA0686E428E" USERNAME = "greenenergy@vedavathi.com" PASSWORD = "Vedavathi@04" # Ensure this is correct without the security token SECURITY_TOKEN = "qe4His8AcuFJucZz5NBHfGU" # Your Salesforce Security Token TOKEN_URL = "https://login.salesforce.com/services/oauth2/token" # Use for production; change to test.salesforce.com for sandbox API_VERSION = "v60.0" # ---------------------- AUTH ---------------------- @st.cache_data def get_salesforce_token(): data = { "grant_type": "password", # Corrected grant_type "client_id": CLIENT_ID, "client_secret": CLIENT_SECRET, "username": USERNAME, "password": PASSWORD + SECURITY_TOKEN # Concatenate password and security token correctly } response = requests.post(TOKEN_URL, data=data) if response.status_code != 200: # Log detailed error message for better debugging error_message = response.json() if response.status_code != 200 else "No error message" st.error(f"Authentication failed! Status code: {response.status_code}, Message: {error_message}") return None, None res = response.json() return res["access_token"], res["instance_url"] # ---------------------- FETCH DATA ---------------------- def fetch_pole_data(instance_url, access_token): headers = { "Authorization": f"Bearer {access_token}" } query = "SELECT Name, Location_Latitude__c, Location_Longitude__c, Camera_Status__c FROM Pole__c LIMIT 100" url = f"{instance_url}/services/data/{API_VERSION}/query?q={query}" response = requests.get(url, headers=headers) if response.status_code != 200: st.error(f"Failed to fetch Pole data! Status code: {response.status_code}, Message: {response.json()}") return pd.DataFrame() records = response.json().get("records", []) df = pd.DataFrame(records) return df[["Name", "Location_Latitude__c", "Location_Longitude__c", "Camera_Status__c"]] # ---------------------- UI ---------------------- st.title("🚦Vedavathi Smart Poles Viewer") token, instance_url = get_salesforce_token() if token: if st.button("🔄 Refresh Pole Data"): df = fetch_pole_data(instance_url, token) if not df.empty: st.success("Pole data loaded successfully!") st.dataframe(df, use_container_width=True) else: st.warning("No data found.") else: st.error("Salesforce authentication failed. Check credentials.")