Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -51,113 +51,6 @@ def show_token(): # Shows access_token if available
|
|
51 |
def show_client():
|
52 |
return token_received["client_id"] if token_received["status"] and token_received["client_id"] else ""
|
53 |
|
54 |
-
# --- Function to fetch LinkedIn Token from Bubble.io ---
|
55 |
-
def fetch_linkedin_token_from_bubble(url_user_token_str):
|
56 |
-
"""
|
57 |
-
Fetches LinkedIn access token from Bubble.io API using the state value (url_user_token_str).
|
58 |
-
The token is expected in a 'Raw_text' field as a JSON string, which is then parsed.
|
59 |
-
Updates the global token_received state if successful.
|
60 |
-
Returns status messages for UI update.
|
61 |
-
"""
|
62 |
-
# Initial UI states (in case of early exit or error)
|
63 |
-
current_status = check_status()
|
64 |
-
current_token_display = show_token()
|
65 |
-
current_client_display = show_client()
|
66 |
-
|
67 |
-
bubble_api_key = os.environ.get("Bubble_API")
|
68 |
-
if not bubble_api_key:
|
69 |
-
error_msg = "❌ Bubble API Error: The 'Bubble_API' environment variable is not set."
|
70 |
-
print(error_msg)
|
71 |
-
return error_msg, current_status, current_token_display, current_client_display
|
72 |
-
|
73 |
-
if not url_user_token_str or "not found" in url_user_token_str or "Could not access" in url_user_token_str:
|
74 |
-
return f"ℹ️ No valid user token from URL to query Bubble. ({url_user_token_str})", current_status, current_token_display, current_client_display
|
75 |
-
|
76 |
-
base_url = "https://app.ingaze.ai/version-test/api/1.1/obj/Linkedin_access"
|
77 |
-
constraints = [{"key": "state", "constraint_type": "equals", "value": url_user_token_str}]
|
78 |
-
params = {'constraints': json.dumps(constraints)}
|
79 |
-
headers = {"Authorization": f"Bearer {bubble_api_key}"}
|
80 |
-
|
81 |
-
bubble_api_status_msg = f"Attempting to fetch token from Bubble for state: {url_user_token_str}..."
|
82 |
-
print(bubble_api_status_msg)
|
83 |
-
|
84 |
-
response = None
|
85 |
-
try:
|
86 |
-
response = requests.get(base_url, params=params, headers=headers, timeout=15) # Increased timeout slightly
|
87 |
-
response.raise_for_status()
|
88 |
-
|
89 |
-
data = response.json()
|
90 |
-
results = data.get("response", {}).get("results", [])
|
91 |
-
|
92 |
-
if results:
|
93 |
-
raw_text_from_bubble = results[0].get("Raw_text", None)
|
94 |
-
parsed_token_dict = None
|
95 |
-
|
96 |
-
if raw_text_from_bubble and isinstance(raw_text_from_bubble, str):
|
97 |
-
try:
|
98 |
-
parsed_token_dict = json.loads(raw_text_from_bubble)
|
99 |
-
if not isinstance(parsed_token_dict, dict):
|
100 |
-
bubble_api_status_msg = (f"⚠️ Bubble API: 'Raw_text' field did not contain a valid JSON dictionary string. "
|
101 |
-
f"Content type: {type(raw_text_from_bubble)}, Value: {raw_text_from_bubble}")
|
102 |
-
print(bubble_api_status_msg)
|
103 |
-
parsed_token_dict = None
|
104 |
-
# If it is a dict, parsed_token_dict is now the token dictionary itself
|
105 |
-
except json.JSONDecodeError as e:
|
106 |
-
bubble_api_status_msg = (f"⚠️ Bubble API: Error decoding 'Raw_text' JSON string: {e}. "
|
107 |
-
f"Content: {raw_text_from_bubble}")
|
108 |
-
print(bubble_api_status_msg)
|
109 |
-
parsed_token_dict = None
|
110 |
-
elif raw_text_from_bubble: # It exists but is not a string
|
111 |
-
bubble_api_status_msg = (f"⚠️ Bubble API: 'Raw_text' field was not a string. "
|
112 |
-
f"Type: {type(raw_text_from_bubble)}, Value: {raw_text_from_bubble}")
|
113 |
-
print(bubble_api_status_msg)
|
114 |
-
|
115 |
-
|
116 |
-
if parsed_token_dict and "access_token" in parsed_token_dict:
|
117 |
-
token_received["status"] = True
|
118 |
-
token_received["token"] = parsed_token_dict # Store the entire parsed dictionary
|
119 |
-
token_received["client_id"] = f"Bubble (state: {url_user_token_str})"
|
120 |
-
bubble_api_status_msg = f"✅ LinkedIn Token successfully fetched and parsed from Bubble 'Raw_text' for state: {url_user_token_str}"
|
121 |
-
print(bubble_api_status_msg)
|
122 |
-
elif raw_text_from_bubble and not parsed_token_dict:
|
123 |
-
# Error message already set by parsing logic if raw_text_from_bubble existed but parsing failed.
|
124 |
-
# If bubble_api_status_msg wasn't set by specific parsing errors, use a general one.
|
125 |
-
if not bubble_api_status_msg.startswith("⚠️"): # Avoid overwriting specific parsing error
|
126 |
-
bubble_api_status_msg = f"⚠️ Bubble API: 'Raw_text' found but could not be parsed into a valid token dictionary for state: {url_user_token_str}."
|
127 |
-
print(bubble_api_status_msg)
|
128 |
-
elif not raw_text_from_bubble:
|
129 |
-
bubble_api_status_msg = (f"⚠️ Bubble API: Token field ('Raw_text') "
|
130 |
-
f"not found or is null in response for state: {url_user_token_str}. Result: {results[0]}")
|
131 |
-
print(bubble_api_status_msg)
|
132 |
-
elif parsed_token_dict and "access_token" not in parsed_token_dict: # Parsed OK, but missing the crucial key
|
133 |
-
bubble_api_status_msg = (f"⚠️ Bubble API: 'access_token' key missing in parsed 'Raw_text' dictionary for state: {url_user_token_str}. Parsed: {parsed_token_dict}")
|
134 |
-
print(bubble_api_status_msg)
|
135 |
-
# If none of the above, the initial bubble_api_status_msg will be used or an error below will catch it.
|
136 |
-
|
137 |
-
else: # No results from Bubble for the given state
|
138 |
-
bubble_api_status_msg = f"❌ Bubble API: No results found for state: {url_user_token_str}"
|
139 |
-
print(bubble_api_status_msg)
|
140 |
-
|
141 |
-
except requests.exceptions.HTTPError as http_err:
|
142 |
-
error_details = response.text if response else "No response content"
|
143 |
-
bubble_api_status_msg = f"❌ Bubble API HTTP error: {http_err} - Response: {error_details}"
|
144 |
-
print(bubble_api_status_msg)
|
145 |
-
except requests.exceptions.Timeout:
|
146 |
-
bubble_api_status_msg = "❌ Bubble API Request timed out."
|
147 |
-
print(bubble_api_status_msg)
|
148 |
-
except requests.exceptions.RequestException as req_err:
|
149 |
-
bubble_api_status_msg = f"❌ Bubble API Request error: {req_err}"
|
150 |
-
print(bubble_api_status_msg)
|
151 |
-
except json.JSONDecodeError as json_err: # Error decoding the main Bubble response, not Raw_text
|
152 |
-
error_details = response.text if response else "No response content"
|
153 |
-
bubble_api_status_msg = f"❌ Bubble API main response JSON decode error: {json_err}. Response: {error_details}"
|
154 |
-
print(bubble_api_status_msg)
|
155 |
-
except Exception as e:
|
156 |
-
bubble_api_status_msg = f"❌ An unexpected error occurred while fetching from Bubble: {str(e)}"
|
157 |
-
print(bubble_api_status_msg)
|
158 |
-
|
159 |
-
# Return values to update all relevant UI components
|
160 |
-
return bubble_api_status_msg, check_status(), show_token(), show_client()
|
161 |
|
162 |
|
163 |
# --- Guarded fetch functions (using token from POST or Bubble) ---
|
|
|
51 |
def show_client():
|
52 |
return token_received["client_id"] if token_received["status"] and token_received["client_id"] else ""
|
53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
|
56 |
# --- Guarded fetch functions (using token from POST or Bubble) ---
|