python-withings
python-withings copied to clipboard
Authorise once and run script as a cron job. Is it possible?
Each time I run this script requires copy/pasting the url and authorizing the application, is it possible to only have to authorize the script once and get a long lived token and then rerun the script to return measurements from withings without having to manually authorize every time? I would like to run this script as a scheduled cron job and pump the values into my own database. Any help would be much appreciated.
@alaverty You can pickle the credentials to a file so that you only need to do it once, then it'll reuse the same credentials every time afterwards.
try:
with open(sys.path[0] + '/pickled_creds', 'rb') as pf:
credentials = pickle.load(pf)
except FileNotFoundError:
auth = WithingsAuth(CONSUMER_KEY, CONSUMER_SECRET)
authorize_url = auth.get_authorize_url()
print("Go to %s allow the app and copy your oauth_verifier" % authorize_url)
oauth_verifier = input('Please enter your oauth_verifier: ')
credentials = auth.get_credentials(oauth_verifier)
# save new credentials to file
with open('./pickled_creds', 'wb') as pf:
pickle.dump(credentials, pf)
FileNotFoundError don't works on python3 this works for me :
auth = WithingsAuth(CONSUMER_KEY, CONSUMER_SECRET)
try:
with open('./pickled_creds', 'rb') as pf:
credentials = pickle.load(pf)
pass
except IOError:
authorize_url = auth.get_authorize_url()
print("Go to %s allow the app and copy your oauth_verifier" % authorize_url)
oauth_verifier = input('Please enter your oauth_verifier: ')
credentials = auth.get_credentials(oauth_verifier)
# save new credentials to file
with open('./pickled_creds', 'wb') as pf:
pickle.dump(credentials, pf)
pass
client = WithingsApi(credentials)