fix token error
Browse files- app/facebook.py +3 -4
- app/sheets.py +21 -12
app/facebook.py
CHANGED
|
@@ -62,10 +62,9 @@ class FacebookClient:
|
|
| 62 |
}
|
| 63 |
|
| 64 |
try:
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
return response.json()
|
| 69 |
except httpx.HTTPError as e:
|
| 70 |
logger.error(f"Error sending message to Facebook: {e}")
|
| 71 |
raise HTTPException(status_code=500, detail="Failed to send message to Facebook")
|
|
|
|
| 62 |
}
|
| 63 |
|
| 64 |
try:
|
| 65 |
+
response = await self._client.post(url, json=payload)
|
| 66 |
+
response.raise_for_status()
|
| 67 |
+
return response.json()
|
|
|
|
| 68 |
except httpx.HTTPError as e:
|
| 69 |
logger.error(f"Error sending message to Facebook: {e}")
|
| 70 |
raise HTTPException(status_code=500, detail="Failed to send message to Facebook")
|
app/sheets.py
CHANGED
|
@@ -7,6 +7,8 @@ import os
|
|
| 7 |
import pickle
|
| 8 |
from datetime import datetime
|
| 9 |
from loguru import logger
|
|
|
|
|
|
|
| 10 |
|
| 11 |
from .utils import timing_decorator_sync
|
| 12 |
from .constants import SHEET_RANGE
|
|
@@ -30,23 +32,30 @@ class SheetsClient:
|
|
| 30 |
def authenticate(self) -> None:
|
| 31 |
"""
|
| 32 |
Xác thực với Google Sheets API, tạo self.service.
|
|
|
|
| 33 |
Input: None
|
| 34 |
Output: None (raise exception nếu lỗi)
|
| 35 |
"""
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
|
| 51 |
self.service = build('sheets', 'v4', credentials=self.creds)
|
| 52 |
|
|
|
|
| 7 |
import pickle
|
| 8 |
from datetime import datetime
|
| 9 |
from loguru import logger
|
| 10 |
+
import json
|
| 11 |
+
from google.oauth2.service_account import Credentials
|
| 12 |
|
| 13 |
from .utils import timing_decorator_sync
|
| 14 |
from .constants import SHEET_RANGE
|
|
|
|
| 32 |
def authenticate(self) -> None:
|
| 33 |
"""
|
| 34 |
Xác thực với Google Sheets API, tạo self.service.
|
| 35 |
+
Đọc credentials từ biến môi trường GOOGLE_SHEETS_CREDENTIALS_JSON nếu có, nếu không thì dùng file.
|
| 36 |
Input: None
|
| 37 |
Output: None (raise exception nếu lỗi)
|
| 38 |
"""
|
| 39 |
+
credentials_json = os.getenv("GOOGLE_SHEETS_CREDENTIALS_JSON")
|
| 40 |
+
if credentials_json:
|
| 41 |
+
info = json.loads(credentials_json)
|
| 42 |
+
creds = Credentials.from_service_account_info(info, scopes=SCOPES)
|
| 43 |
+
self.creds = creds
|
| 44 |
+
else:
|
| 45 |
+
if os.path.exists(self.token_file):
|
| 46 |
+
with open(self.token_file, 'rb') as token:
|
| 47 |
+
self.creds = pickle.load(token)
|
| 48 |
|
| 49 |
+
if not self.creds or not self.creds.valid:
|
| 50 |
+
if self.creds and self.creds.expired:
|
| 51 |
+
self.creds.refresh(Request())
|
| 52 |
+
else:
|
| 53 |
+
flow = InstalledAppFlow.from_client_secrets_file(
|
| 54 |
+
self.credentials_file, SCOPES)
|
| 55 |
+
self.creds = flow.run_local_server(port=0)
|
| 56 |
|
| 57 |
+
with open(self.token_file, 'wb') as token:
|
| 58 |
+
pickle.dump(self.creds, token)
|
| 59 |
|
| 60 |
self.service = build('sheets', 'v4', credentials=self.creds)
|
| 61 |
|