App registration works

main
Mike Lynch 2024-12-14 16:50:26 +11:00
parent d1633d8040
commit 471285d7d7
1 changed files with 23 additions and 16 deletions

View File

@ -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__":