Skip to content

Instantly share code, notes, and snippets.

@alexcreek
Last active June 23, 2022 01:14
Show Gist options
  • Save alexcreek/ff96999a44b5e3ab63712c7d236446a0 to your computer and use it in GitHub Desktop.
Save alexcreek/ff96999a44b5e3ab63712c7d236446a0 to your computer and use it in GitHub Desktop.
Use your existing tdameritrade refresh_token to create a new refresh_token valid for another 90 days
#!/usr/bin/env python3
# https://developer.tdameritrade.com/content/authentication-faq
from requests import post
import os
import sys
try:
refresh_token = os.environ['REFRESH_TOKEN']
client_id = os.environ['CLIENT_ID']
except KeyError as e:
print(f'{e} environment variable not found')
sys.exit(1)
payload = {
'grant_type': 'refresh_token',
'refresh_token': refresh_token,
'access_type': 'offline',
'client_id': client_id
}
auth_url = os.getenv('TD_AUTH_URL', 'https://api.tdameritrade.com/v1/oauth2/token')
timeout = int(os.getenv('TD_TIMEOUT', '30'))
resp = post(auth_url, data=payload, timeout=timeout)
print(resp.json())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment