Spaces:
Sleeping
Sleeping
hf-actions
commited on
Commit
·
0020de5
1
Parent(s):
e8bb202
feat: improve Facebook token validation (debug_token), log detailed reasons and classify expiry
Browse files
app.py
CHANGED
|
@@ -152,26 +152,77 @@ if __name__ == "__main__":
|
|
| 152 |
logger.error("Facebook token or page id missing")
|
| 153 |
ok = False
|
| 154 |
else:
|
| 155 |
-
#
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
r = requests.get(f"https://graph.facebook.com/me?access_token={fb_token}", timeout=10)
|
| 162 |
if r.status_code == 200:
|
| 163 |
-
|
| 164 |
-
break
|
| 165 |
else:
|
|
|
|
|
|
|
| 166 |
logger.warning("Facebook token validation returned %s: %s", r.status_code, r.text)
|
| 167 |
-
|
| 168 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
except Exception:
|
| 170 |
-
logger.
|
| 171 |
-
_time.sleep(2)
|
| 172 |
-
continue
|
| 173 |
-
if not _ok:
|
| 174 |
-
logger.exception("Facebook token validation failed")
|
| 175 |
ok = False
|
| 176 |
|
| 177 |
if not rep_token:
|
|
|
|
| 152 |
logger.error("Facebook token or page id missing")
|
| 153 |
ok = False
|
| 154 |
else:
|
| 155 |
+
# Try to inspect the token using the Graph API debug_token endpoint when possible
|
| 156 |
+
# This provides detailed info (is_valid, expires_at, type) when app credentials are available
|
| 157 |
+
fb_app_id = os.getenv("FB_APP_ID")
|
| 158 |
+
fb_app_secret = os.getenv("FB_APP_SECRET")
|
| 159 |
+
debug_info = None
|
| 160 |
+
try:
|
| 161 |
+
if fb_app_id and fb_app_secret:
|
| 162 |
+
app_access = f"{fb_app_id}|{fb_app_secret}"
|
| 163 |
+
dbg_url = f"https://graph.facebook.com/debug_token?input_token={fb_token}&access_token={app_access}"
|
| 164 |
+
rdbg = requests.get(dbg_url, timeout=10)
|
| 165 |
+
if rdbg.status_code == 200:
|
| 166 |
+
debug_info = rdbg.json().get("data")
|
| 167 |
+
else:
|
| 168 |
+
logger.warning("Facebook debug_token returned %s: %s", rdbg.status_code, rdbg.text)
|
| 169 |
+
# If debug_token not used or failed, fall back to /me check
|
| 170 |
+
if debug_info is None:
|
| 171 |
r = requests.get(f"https://graph.facebook.com/me?access_token={fb_token}", timeout=10)
|
| 172 |
if r.status_code == 200:
|
| 173 |
+
debug_info = {"is_valid": True}
|
|
|
|
| 174 |
else:
|
| 175 |
+
# log detailed failure
|
| 176 |
+
reason = f"/me check returned {r.status_code}: {r.text}"
|
| 177 |
logger.warning("Facebook token validation returned %s: %s", r.status_code, r.text)
|
| 178 |
+
try:
|
| 179 |
+
with open("log.txt", "a", encoding="utf-8") as lf:
|
| 180 |
+
lf.write(f"[{__import__('time').strftime('%Y-%m-%d %H:%M:%S')}] FB_TOKEN_VALIDATION_ERROR reason={reason}\n")
|
| 181 |
+
except Exception:
|
| 182 |
+
logger.exception("Failed to write token validation failure to log.txt")
|
| 183 |
+
ok = False
|
| 184 |
+
# If we have debug_info, examine it for validity and expiration
|
| 185 |
+
if debug_info:
|
| 186 |
+
is_valid = debug_info.get("is_valid", False)
|
| 187 |
+
if not is_valid:
|
| 188 |
+
reason = f"debug_token reports invalid: {debug_info}"
|
| 189 |
+
logger.error(reason)
|
| 190 |
+
try:
|
| 191 |
+
with open("log.txt", "a", encoding="utf-8") as lf:
|
| 192 |
+
lf.write(f"[{__import__('time').strftime('%Y-%m-%d %H:%M:%S')}] FB_TOKEN_VALIDATION_ERROR reason={reason}\n")
|
| 193 |
+
except Exception:
|
| 194 |
+
logger.exception("Failed to write token validation failure to log.txt")
|
| 195 |
+
ok = False
|
| 196 |
+
else:
|
| 197 |
+
# determine expiry classification if available
|
| 198 |
+
expires_at = debug_info.get("expires_at")
|
| 199 |
+
if expires_at:
|
| 200 |
+
try:
|
| 201 |
+
expires_at = int(expires_at)
|
| 202 |
+
import time as _time
|
| 203 |
+
delta = expires_at - int(_time.time())
|
| 204 |
+
days = max(0, delta // 86400)
|
| 205 |
+
if days >= 30:
|
| 206 |
+
token_type = f"long-lived (~{days} days)"
|
| 207 |
+
else:
|
| 208 |
+
token_type = f"short-lived (~{days} days)"
|
| 209 |
+
except Exception:
|
| 210 |
+
token_type = "unknown-expiry"
|
| 211 |
+
else:
|
| 212 |
+
token_type = "non-expiring-or-page-token"
|
| 213 |
+
logger.info("Facebook token looks valid (%s)", token_type)
|
| 214 |
+
try:
|
| 215 |
+
with open("log.txt", "a", encoding="utf-8") as lf:
|
| 216 |
+
lf.write(f"[{__import__('time').strftime('%Y-%m-%d %H:%M:%S')}] FB_TOKEN_VALIDATION_OK type={token_type}\n")
|
| 217 |
+
except Exception:
|
| 218 |
+
logger.exception("Failed to write token validation ok to log.txt")
|
| 219 |
+
except Exception as e:
|
| 220 |
+
logger.exception("Facebook token validation failed with exception")
|
| 221 |
+
try:
|
| 222 |
+
with open("log.txt", "a", encoding="utf-8") as lf:
|
| 223 |
+
lf.write(f"[{__import__('time').strftime('%Y-%m-%d %H:%M:%S')}] FB_TOKEN_VALIDATION_EXCEPTION {e}\n")
|
| 224 |
except Exception:
|
| 225 |
+
logger.exception("Failed to write token validation exception to log.txt")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 226 |
ok = False
|
| 227 |
|
| 228 |
if not rep_token:
|