Spaces:
Sleeping
Sleeping
Update token_manager.py
Browse files- token_manager.py +20 -9
token_manager.py
CHANGED
|
@@ -1,28 +1,39 @@
|
|
| 1 |
import requests
|
| 2 |
-
import
|
| 3 |
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
def refresh_snapchat_token():
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
data = {
|
| 10 |
"grant_type": "refresh_token",
|
| 11 |
-
"refresh_token":
|
| 12 |
"client_id": os.getenv("CLIENT_ID"),
|
| 13 |
"client_secret": os.getenv("CLIENT_SECRET")
|
| 14 |
}
|
| 15 |
|
| 16 |
headers = {"Content-Type": "application/x-www-form-urlencoded"}
|
| 17 |
url = "https://accounts.snapchat.com/login/oauth2/access_token"
|
| 18 |
-
|
| 19 |
response = requests.post(url, headers=headers, data=data)
|
| 20 |
res_data = response.json()
|
| 21 |
|
| 22 |
if "access_token" in res_data:
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
| 27 |
else:
|
| 28 |
print("❌ Failed to refresh token:", res_data)
|
|
|
|
| 1 |
import requests
|
| 2 |
+
import os
|
| 3 |
from datetime import datetime
|
| 4 |
+
from supabase import create_client
|
| 5 |
+
|
| 6 |
+
SUPABASE_URL = os.getenv("SUPABASE_URL")
|
| 7 |
+
SUPABASE_API_KEY = os.getenv("SUPABASE_API_KEY")
|
| 8 |
+
supabase = create_client(SUPABASE_URL, SUPABASE_API_KEY)
|
| 9 |
|
| 10 |
def refresh_snapchat_token():
|
| 11 |
+
# Get latest token from Supabase
|
| 12 |
+
result = supabase.table("snapchat_tokens").select("*").order("refreshed_at", desc=True).limit(1).execute()
|
| 13 |
+
if not result.data:
|
| 14 |
+
print("❌ No tokens found in Supabase")
|
| 15 |
+
return
|
| 16 |
+
|
| 17 |
+
latest = result.data[0]
|
| 18 |
|
| 19 |
data = {
|
| 20 |
"grant_type": "refresh_token",
|
| 21 |
+
"refresh_token": latest["refresh_token"],
|
| 22 |
"client_id": os.getenv("CLIENT_ID"),
|
| 23 |
"client_secret": os.getenv("CLIENT_SECRET")
|
| 24 |
}
|
| 25 |
|
| 26 |
headers = {"Content-Type": "application/x-www-form-urlencoded"}
|
| 27 |
url = "https://accounts.snapchat.com/login/oauth2/access_token"
|
|
|
|
| 28 |
response = requests.post(url, headers=headers, data=data)
|
| 29 |
res_data = response.json()
|
| 30 |
|
| 31 |
if "access_token" in res_data:
|
| 32 |
+
supabase.table("snapchat_tokens").insert({
|
| 33 |
+
"access_token": res_data["access_token"],
|
| 34 |
+
"refresh_token": res_data["refresh_token"],
|
| 35 |
+
"refreshed_at": datetime.utcnow().isoformat()
|
| 36 |
+
}).execute()
|
| 37 |
+
print(f"✅ Token refreshed and saved at {datetime.utcnow().isoformat()}")
|
| 38 |
else:
|
| 39 |
print("❌ Failed to refresh token:", res_data)
|