Complete access token cycle tested
parent
471285d7d7
commit
1e143f35bc
|
@ -38,6 +38,11 @@ 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}")
|
||||||
|
auth_url = f"{self.base_url}/oauth/authorize?client_id={self.client_id}&redirect_uri=urn:ietf:wg:oauth:2.0:oob&response_type=code&scope={self.scopes}"
|
||||||
|
print("Visit this URL in a private browser window to authenticate:")
|
||||||
|
print(auth_url)
|
||||||
|
print("\nThen run this script again with the OOB token, like:")
|
||||||
|
print("> uv run register -u {self.base_ur} -n {self.name} -t OOB_TOKEB")
|
||||||
return True
|
return True
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("Something went wrong:")
|
print("Something went wrong:")
|
||||||
|
@ -45,14 +50,62 @@ class Application:
|
||||||
print(data)
|
print(data)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
def get_auth_token(self, token):
|
||||||
|
app_json = Path(f"./{self.name}_app.json")
|
||||||
|
if not app_json.is_file():
|
||||||
|
print(f"No {app_json} file found")
|
||||||
|
print("You need to register the app before getting a token")
|
||||||
|
return False
|
||||||
|
try:
|
||||||
|
with open(app_json, "r") as jfh:
|
||||||
|
cf = json.load(jfh)
|
||||||
|
data = {
|
||||||
|
"client_id": cf["client_id"],
|
||||||
|
"client_secret": cf["client_secret"],
|
||||||
|
"redirect_uri": "urn:ietf:wg:oauth:2.0:oob",
|
||||||
|
"grant_type": "authorization_code",
|
||||||
|
"code": token,
|
||||||
|
}
|
||||||
|
response = requests.post(
|
||||||
|
f"{self.base_url}/oauth/token",
|
||||||
|
data=json.dumps(data),
|
||||||
|
headers=self.headers,
|
||||||
|
)
|
||||||
|
response.raise_for_status()
|
||||||
|
json_r = response.json()
|
||||||
|
access_token = json_r["access_token"]
|
||||||
|
print(f"Got access token: {access_token}")
|
||||||
|
at_json = self.unique_at_file()
|
||||||
|
with open(at_json, "w") as jfh:
|
||||||
|
json.dump(json_r, jfh, indent=2)
|
||||||
|
print(f"Wrote response to {at_json}")
|
||||||
|
except Exception as e:
|
||||||
|
print("Something went wrong:")
|
||||||
|
print(e)
|
||||||
|
print(data)
|
||||||
|
return False
|
||||||
|
|
||||||
|
def unique_at_file(self):
|
||||||
|
fn = f"./{self.name}_at.json"
|
||||||
|
n = 0
|
||||||
|
while Path(fn).is_file():
|
||||||
|
print(f"File {fn} already exists")
|
||||||
|
n += 1
|
||||||
|
fn = f"./{self.name}_at_{n}.json"
|
||||||
|
return fn
|
||||||
|
|
||||||
|
|
||||||
def cli():
|
def cli():
|
||||||
ap = argparse.ArgumentParser()
|
ap = argparse.ArgumentParser()
|
||||||
ap.add_argument("-u", "--url", 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.add_argument("-s", "--scopes", default="write", type=str, help="Scope")
|
ap.add_argument("-s", "--scopes", default="write", type=str, help="Scope")
|
||||||
|
ap.add_argument("-t", "--token", type=str, help="OOB token")
|
||||||
args = ap.parse_args()
|
args = ap.parse_args()
|
||||||
app = Application(args.url, args.name, args.scopes)
|
app = Application(args.url, args.name, args.scopes)
|
||||||
|
if args.token:
|
||||||
|
app.get_auth_token(args.token)
|
||||||
|
else:
|
||||||
app.register()
|
app.register()
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue