4월, 2025의 게시물 표시

키움증권 REST API 인증 시작하기 - 토큰 발급 및 폐기 방법

키움증권 REST API 인증 시작하기 - 토큰 발급 및 폐기 방법 키움증권 REST API 인증 시작하기 – 토큰 발급과 폐기 실습 📚 목차 1️⃣ 접근 토큰 발급 2️⃣ 접근 토큰 폐기 1️⃣ 접근 토큰 발급 키움 OpenAPI의 REST 방식으로 API를 호출하려면 먼저 접근 토큰(access token) 을 발급받아야 합니다. 아래는 파이썬 코드 예시입니다. import requests url = "https://openapi.koreainvestment.com:9443/oauth2/tokenP" headers = { "content-type": "application/json" } body = { "grant_type": "client_credentials", "appkey": "앱키를 입력하세요", "appsecret": "앱시크릿을 입력하세요" } res = requests.post(url, headers=headers, json=body) print(res.json()) 응답 예시: { "access_token": "abcdefg...1234", "token_type": "Bearer", "expires_in": "1800" } 2️⃣ 접근 토큰 폐기 발급받은 토큰은 사용이 끝난 후 보안을 위해 폐기(revoke) 하는 것이 권장됩니다. url = "https://openapi.koreainvestment.com:9443/oauth2/revokeP" headers = {"conten...