File size: 2,746 Bytes
d0258ad
 
 
7ab2503
d0258ad
 
b3f239a
 
d0258ad
7ab2503
 
7e02afa
d0258ad
 
 
 
 
 
7ab2503
 
 
 
 
d0258ad
 
7e02afa
d0258ad
7e02afa
 
 
d0258ad
7e02afa
d0258ad
 
 
 
 
 
 
 
7ab2503
d0258ad
 
7e02afa
d0258ad
7e02afa
d0258ad
7e02afa
d0258ad
 
7ab2503
d0258ad
 
f94f139
d0258ad
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import streamlit as st
import requests
import pandas as pd
import simple_salesforce

# ---------------------- CONFIG ----------------------
CLIENT_ID = "3MVG9VMBZCsTL9hnfx2eVMOHa56mwSZnvuAnPr3kVVBEQfeLYvrSfJNRRjjSlKWPLy99XM6kefg=="
CLIENT_SECRET = "6F7E9C95CE20CC07FC1EBD39B34909739D99975A0EEB548240926EA0686E428E"
USERNAME = "[email protected]"
PASSWORD = "Vedavathi@04"  # Keep this as the password (without the security token)
SECURITY_TOKEN = "qe4His8AcuFJucZz5NBHfGU"  # Your Salesforce Security Token
TOKEN_URL = "https://login.salesforce.com/services/oauth2/token"  # For production
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  # Correctly concatenate password and security token
    }
    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.")