diff --git a/src/farmbot_yolo/request_token.py b/src/farmbot_yolo/request_token.py index 8c1622e25054401f12f4e89b1289d2b28de17c46..ef793f927a00fa57635ec4f13c74816e0a939bbd 100644 --- a/src/farmbot_yolo/request_token.py +++ b/src/farmbot_yolo/request_token.py @@ -1,37 +1,28 @@ -"""Request a token to store in creds.json.""" +#!/usr/bin/env python3 + +# request a token (for creds.py) import argparse import json from urllib import request -from farmbot_yolo import CREDS_PATH - - -def request_token(email: str, password: str): - auth_info = {'user': {'email': email, 'password': password}} - - req = request.Request('https://my.farmbot.io/api/tokens') - req.add_header('Content-Type', 'application/json') - response = request.urlopen(req, data=json.dumps(auth_info).encode('utf-8')) - - token_info = json.loads(response.read().decode()) - - print("mqtt host [%s]" % token_info['token']['unencoded']['mqtt']) - - creds_dict = {"device_id": token_info["token"]["unencoded"]["bot"], - "token": token_info['token']['encoded']} +parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter) +parser.add_argument('--email', type=str, help="user email for token request") +parser.add_argument('--password', type=str, help="user password for token request") +opts = parser.parse_args() +print("opts %s" % opts) - print("rewriting creds.json") - with CREDS_PATH.open(mode="w") as fp: - json.dump(creds_dict, fp) +auth_info = {'user': {'email': opts.email, 'password': opts.password }} +req = request.Request('https://my.farmbot.io/api/tokens') +req.add_header('Content-Type', 'application/json') +response = request.urlopen(req, data=json.dumps(auth_info).encode('utf-8')) -if __name__ == "__main__": - parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter) - parser.add_argument('--email', type=str, help="user email for token request") - parser.add_argument('--password', type=str, help="user password for token request") - opts = parser.parse_args() +token_info = json.loads(response.read().decode()) - print("opts %s" % opts) +print("mqtt host [%s]" % token_info['token']['unencoded']['mqtt']) - request_token(opts.email, opts.password) +print("rewriting creds.py") +with open("creds.py", "w") as f: + f.write("device_id=\"%s\"\n" % token_info['token']['unencoded']['bot']) + f.write("token=\"%s\"\n" % token_info['token']['encoded'])