42 lines
1.7 KiB
Python
42 lines
1.7 KiB
Python
|
|
import urllib.parse
|
||
|
|
|
||
|
|
import requests
|
||
|
|
|
||
|
|
from src.config import Config
|
||
|
|
|
||
|
|
|
||
|
|
class TickTick:
|
||
|
|
def __init__(self) -> None:
|
||
|
|
print("Initializing TickTick...")
|
||
|
|
if Config.get_env("TICKTICK_ACCESS_TOKEN") is None:
|
||
|
|
self._begin_auth()
|
||
|
|
|
||
|
|
def _begin_auth(self) -> None:
|
||
|
|
ticktick_code_auth_url = "https://ticktick.com/oauth/authorize?"
|
||
|
|
ticktick_code_auth_params = {
|
||
|
|
"client_id": Config.get_env("TICKTICK_CLIENT_ID"),
|
||
|
|
"scope": "tasks:read tasks:write",
|
||
|
|
"state": "begin_auth",
|
||
|
|
"redirect_uri": Config.get_env("TICKTICK_CODE_REDIRECT_URI"),
|
||
|
|
"response_type": "code",
|
||
|
|
}
|
||
|
|
ticktick_auth_url_encoded = urllib.parse.urlencode(ticktick_code_auth_params)
|
||
|
|
print("Visit: ", ticktick_code_auth_url + ticktick_auth_url_encoded, " to authenticate.")
|
||
|
|
|
||
|
|
def retrieve_access_token(self, code: str, state: str) -> bool:
|
||
|
|
if state != "begin_auth":
|
||
|
|
print("Invalid state.")
|
||
|
|
return False
|
||
|
|
ticktick_token_url = "https://ticktick.com/oauth/token" # noqa: S105
|
||
|
|
ticktick_token_auth_params = {
|
||
|
|
"code": code,
|
||
|
|
"grant_type": "authorization_code",
|
||
|
|
"scope": "tasks:write tasks:read",
|
||
|
|
"redirect_uri": Config.get_env("TICKTICK_CODE_REDIRECT_URI"),
|
||
|
|
}
|
||
|
|
client_id = Config.get_env("TICKTICK_CLIENT_ID")
|
||
|
|
client_secret = Config.get_env("TICKTICK_CLIENT_SECRET")
|
||
|
|
response = requests.post(ticktick_token_url, data=ticktick_token_auth_params, auth=(client_id, client_secret), timeout=10)
|
||
|
|
Config.update_env("TICKTICK_ACCESS_TOKEN", response.json().get("access_token"))
|
||
|
|
return True
|