App registration works
parent
d1633d8040
commit
471285d7d7
|
@ -7,27 +7,29 @@ import argparse
|
||||||
|
|
||||||
|
|
||||||
class Application:
|
class Application:
|
||||||
def __init__(self, base_url, name):
|
def __init__(self, base_url, name, scopes="write"):
|
||||||
self.base_url = base_url
|
self.base_url = base_url
|
||||||
self.name = name
|
self.name = name
|
||||||
|
self.scopes = scopes
|
||||||
self.headers = {"Content-Type": "application/json"}
|
self.headers = {"Content-Type": "application/json"}
|
||||||
|
|
||||||
def register_app(self):
|
def register(self):
|
||||||
app_json = Path(f"./{self.name}_app.json")
|
app_json = Path(f"./{self.name}_app.json")
|
||||||
if Path.isfile():
|
if app_json.is_file():
|
||||||
print(f"Found {app_json} file")
|
print(f"Found {app_json} file")
|
||||||
print(f"Looks like you've already registered {self.name}")
|
print(f"Looks like you've already registered {self.name}")
|
||||||
return False
|
return False
|
||||||
response = requests.post(
|
data = {
|
||||||
f"{self.base_url}/api/v1/statuses",
|
"client_name": self.name,
|
||||||
data={
|
"redirect_uris": "urn:ietf:wg:oauth:2.0:oob",
|
||||||
"client_name": self.name,
|
"scopes": self.scopes,
|
||||||
"redirect_uris": "urn:ietf:wg:oauth:2.0:oob",
|
}
|
||||||
"scopes": "write",
|
|
||||||
},
|
|
||||||
headers=self.headers,
|
|
||||||
)
|
|
||||||
try:
|
try:
|
||||||
|
response = requests.post(
|
||||||
|
f"{self.base_url}/api/v1/apps",
|
||||||
|
data=json.dumps(data),
|
||||||
|
headers=self.headers,
|
||||||
|
)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
json_r = response.json()
|
json_r = response.json()
|
||||||
self.client_id = json_r["client_id"]
|
self.client_id = json_r["client_id"]
|
||||||
|
@ -36,17 +38,22 @@ class Application:
|
||||||
with open(app_json, "w") as jfh:
|
with open(app_json, "w") as jfh:
|
||||||
json.dump(json_r, jfh, indent=2)
|
json.dump(json_r, jfh, indent=2)
|
||||||
print(f"Wrote response to {app_json}")
|
print(f"Wrote response to {app_json}")
|
||||||
|
return True
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("Something went wrong...")
|
print("Something went wrong:")
|
||||||
print(e)
|
print(e)
|
||||||
|
print(data)
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
def cli():
|
def cli():
|
||||||
ap = argparse.ArgumentParser()
|
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.add_argument("-n", "--name", required=True, type=str, help="Application name")
|
||||||
ap.parse_args()
|
ap.add_argument("-s", "--scopes", default="write", type=str, help="Scope")
|
||||||
print(ap)
|
args = ap.parse_args()
|
||||||
|
app = Application(args.url, args.name, args.scopes)
|
||||||
|
app.register()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
Loading…
Reference in New Issue