from xdk.auth import OAuth2PKCE
from urllib.parse import urlparse
import webbrowser
# 1단계: PKCE 인스턴스 생성
auth = OAuth2PKCE(
client_id="your_client_id",
redirect_uri="http://localhost:8080/callback",
scopes=["tweet.read", "users.read", "offline.access"] # 필요에 따라 스코프를 조정하세요
)
# 2단계: 인가 URL 가져오기
auth_url = auth.get_authorization_url()
print(f"인가를 위해 이 URL을 방문하세요: {auth_url}")
webbrowser.open(auth_url)
# 3단계: 콜백 처리(실제 App에서는 Flask 같은 웹 프레임워크 사용)
# 예: callback_url = "http://localhost:8080/callback?code=AUTH_CODE_HERE"
callback_url = input("전체 콜백 URL을 여기 붙여넣으세요: ")
parsed = urlparse(callback_url)
code = parsed.query.split("=")[1]
# 4단계: 코드를 토큰으로 교환
tokens = auth.fetch_token(authorization_code=code)
access_token = tokens["access_token"]
refresh_token = tokens["refresh_token"] # 갱신용으로 저장
# 5단계: 클라이언트 생성
client = Client(oauth2_access_token=access_token)