#!/usr/bin/env python

import sys
import json
from ttlockwrapper.ttlock import TTlockAPIError
from ttlockwrapper import TTLock,constants

'''
To call this script:
$ pip3 install ttlockio
$ refresh_access_token YOUR_TTLOCK_CLIENT_ID YOUR_TTLOCK_CLIENT_SECRET REFRESH_TOKEN REDIRECT_URI
For "YOUR_TTLOCK_CLIENT_ID" and "YOUR_TTLOCK_CLIENT_SECRET", register at https://open.ttlock.com/reg 
"REFRESH_TOKEN" Refresh token
"REDIRECT_URI" Ex: https://yourdomain.com/
'''

clientId = str(sys.argv[1])
clientSecret = str(sys.argv[2])
refreshToken = str(sys.argv[3])
redirect_url = str(sys.argv[4])

result=TTLock.refresh_token(clientId,clientSecret,refreshToken,redirect_url)
if result.get(constants.EXPIRE_TIME_TOKEN_FIELD):
    parsed = json.loads(result)
    print(json.dumps(parsed, indent=2, sort_keys=False))
else:
    raise TTlockAPIError()
