From 471285d7d7fb80af184c07e6a03dc2f54d2211a8 Mon Sep 17 00:00:00 2001 From: Mike Lynch Date: Sat, 14 Dec 2024 16:50:26 +1100 Subject: [PATCH] App registration works --- src/gotosocial/register.py | 39 ++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/src/gotosocial/register.py b/src/gotosocial/register.py index 9139ff0..4351d6a 100644 --- a/src/gotosocial/register.py +++ b/src/gotosocial/register.py @@ -7,27 +7,29 @@ import argparse class Application: - def __init__(self, base_url, name): + def __init__(self, base_url, name, scopes="write"): self.base_url = base_url self.name = name + self.scopes = scopes self.headers = {"Content-Type": "application/json"} - def register_app(self): + def register(self): app_json = Path(f"./{self.name}_app.json") - if Path.isfile(): + if app_json.is_file(): print(f"Found {app_json} file") print(f"Looks like you've already registered {self.name}") return False - response = requests.post( - f"{self.base_url}/api/v1/statuses", - data={ - "client_name": self.name, - "redirect_uris": "urn:ietf:wg:oauth:2.0:oob", - "scopes": "write", - }, - headers=self.headers, - ) + data = { + "client_name": self.name, + "redirect_uris": "urn:ietf:wg:oauth:2.0:oob", + "scopes": self.scopes, + } try: + response = requests.post( + f"{self.base_url}/api/v1/apps", + data=json.dumps(data), + headers=self.headers, + ) response.raise_for_status() json_r = response.json() self.client_id = json_r["client_id"] @@ -36,17 +38,22 @@ class Application: with open(app_json, "w") as jfh: json.dump(json_r, jfh, indent=2) print(f"Wrote response to {app_json}") + return True except Exception as e: - print("Something went wrong...") + print("Something went wrong:") print(e) + print(data) + return False def cli(): ap = argparse.ArgumentParser() - ap.add_argument("-s", "--server", required=True, type=str, help="GoToSocial server") + ap.add_argument("-u", "--url", required=True, type=str, help="GoToSocial server") ap.add_argument("-n", "--name", required=True, type=str, help="Application name") - ap.parse_args() - print(ap) + ap.add_argument("-s", "--scopes", default="write", type=str, help="Scope") + args = ap.parse_args() + app = Application(args.url, args.name, args.scopes) + app.register() if __name__ == "__main__":