Skip to content

Instantly share code, notes, and snippets.

@stefan-home
Last active December 11, 2025 18:27
Show Gist options
  • Select an option

  • Save stefan-home/808d8774a461f22f2ae1d9519f067b71 to your computer and use it in GitHub Desktop.

Select an option

Save stefan-home/808d8774a461f22f2ae1d9519f067b71 to your computer and use it in GitHub Desktop.
Get token for Hyundai api using headless terminal

Tool to extract token for hyundai api authorization for use without GUI on headless system

  • Can use on any terminal without GUI (like SSH on remote computer).
  • Run with python. Tested with python3.9. Only requires packages "request and sys" (very common).
  • Start script as: python3 HyundaiTokenTerminal.py.
  • Script is self explaining. It provides some url's to paste into the address-bar of a browser on your main computer and requires you to paste back the response in the script. Script finalises with providing refresh-token (to be used as password in the api) and access-token (not necessary to use).

Note, Important!!!

  • Be sure to set the user-agent of the browser as per instruction of the script. Without correct user-agent the script does not work.
  • Be very sure to use same window/tab for setting user agent and all subsequent url-pastes. If you donot do this you get an error.
#Tool to extract token for hyundai api authorization
# - Can use on any terminal without GUI (like SSH on remote computer).
# - Run with python. Tested with python3.9. Only requires packages "request and sys" (very common).
# - Start script as: python3 HyundaiTokenTerminal.py.
# - Script is self explaining. It provides some url's to paste into the address-bar of a browser on your main computer and requires you to paste back the response in the script. Script finalises with providing refresh-token (to be used as password in the api) and access-token (not necessary to use).
# - Be sure to set the user-agent of the browser as per instruction of the script. Without correct user-agent the script does not work.
import requests
import sys
from urllib.parse import urlparse, parse_qs, quote
user_agent: str = (
"Mozilla/5.0 (Linux; Android 4.1.1; Galaxy Nexus Build/JRO03C) "
"AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19_CCS_APP_AOS"
)
session = requests.Session()
CLIENT_ID = "6d477c38-3ca4-4cf3-9557-2a1929a94654"
CLIENT_SECRET = "KUy49XxPzLpLuoK0xhBC77W6VXhmtQR9iQhmIFjjoY4IpxsV"
BASE_URL = "https://idpconnect-eu.hyundai.com/auth/api/v2/user/oauth2/"
LOGIN_URL = f"{BASE_URL}authorize?client_id=peuhyundaiidm-ctb&redirect_uri=https%3A%2F%2Fctbapi.hyundai-europe.com%2Fapi%2Fauth&nonce=&state=PL_&scope=openid+profile+email+phone&response_type=code&connector_client_id=peuhyundaiidm-ctb&connector_scope=&connector_session_key=&country=&captcha=1&ui_locales=en-US"
SUCCESS_ELEMENT_SELECTOR = "button.mail_check"
REDIRECT_URL_FINAL = "https://prd.eu-ccapi.hyundai.com:8080/api/v1/user/oauth2/token"
REDIRECT_URL = f"{BASE_URL}authorize?response_type=code&client_id={CLIENT_ID}&redirect_uri={REDIRECT_URL_FINAL}&lang=de&state=ccsp"
TOKEN_URL = f"{BASE_URL}token"
def _get_authorization_code(url: str) -> str:
# Retrieve the authorization_code from the url.
try:
url_parsed = urlparse(url)
url_queries = parse_qs(url_parsed.query)
code: str = url_queries["code"][0]
return code
except Exception as e:
print(f"\n❌ Could not extract authorization code query from the URL {url}. Please try again: {e}")
print(f"Mostly things fail because you did not use the same window/tab for user agent and all other actions\n")
print(f"retry and make sure to use a single window/tab for all chrome/safari actions\n")
sys.exit(1)
def main() -> None:
print(f"Step 1: Open a NEW window from your browser, ONLY TO BE USED for this procedure \n"
f" Chrome: View/Developer/Developer tools\n"
f" 'network conditions' is likely open at bottom. if not: 3-dot menu at the right\n"
f" more tools/network conditions\n"
f" uncheck 'Use browser default' next to 'User agent' \n"
f" choose 'custom...' and fill in the following user agent:\n"
f" {user_agent}\n")
print(f" Safari: Safari/Settings/Advanced > at bottom tick 'show features for web developers'\n"
f" menubar of dedicated window: Develop/User Agent/Other...\n"
f" fill in the following user agent and press OK\n"
f" {user_agent}\n\n\n")
input(f"press enter if you have set the user agent\n")
print(f"Step 2: Open this URL in THIS DEDICATED WINDOW/TAB:\n")
print(f" {LOGIN_URL}\n\n\n")
print(f"Step 3: Solve the reCAPTCHA and login with your credentials.\n"
f" After successful login, you get redirected to Hyundai homepage.\n"
f" If you NOT get on Hyundai page you likely did not correctly set the user agent.\n")
confirm: str = input(
" Was the login successful? (y/n): "
).strip().lower()
if confirm != "y":
print(f"❌ Exiting script. Please try again after successful login.")
sys.exit(1)
print(f"\nStep 4: Open the following URL in the SAME browser window/tab where you're logged in:\n")
print(f" {REDIRECT_URL}\n")
current_url: str = input(
f"Step 5: A blank page with maybe \"errCode\":\"4010\" will open with the URL from step 4 which starts with\n "
f" '{BASE_URL}token?code=...&state=ccsp&login_success=y'\n"
f" Copy the full URL from the address bar and paste it here:\n\n"
f" > "
)
code: str = _get_authorization_code(current_url)
data = {
"grant_type": "authorization_code",
"code": code,
"redirect_uri": REDIRECT_URL_FINAL,
"client_id": CLIENT_ID,
"client_secret": CLIENT_SECRET,
}
response = session.post(TOKEN_URL, data=data)
if response.status_code == 200:
tokens = response.json()
if tokens is not None:
refresh_token = tokens["refresh_token"]
access_token = tokens["access_token"]
print(f"\n✅ Your tokens are:\n\n- Refresh Token: {refresh_token}\n- Access Token: {access_token}")
else:
print(f"\n❌ Error getting tokens from the API!\n{response.text}")
print(f"Mostly things fail because you did not use the same window/tab for user agent and all other actions\n")
print(f"retry and make sure to use a single window/tab for all chrome/safari actions\n")
if __name__ == "__main__":
main()
@stefan-home
Copy link
Author

@bussnet : are you sure you carefully followed the directions to set the user agent?
And make sure you set user agent and do all url requests in the same window/tab?

@bussnet
Copy link

bussnet commented Oct 28, 2025

I opened it in the same browser/window, but a new Tab :(... I think it was to late to read exactly and oversee the "tab" behind the browser. Then I tried it with this script and this worked, so it seems the procedure still works, it was just my fault.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment