from flask import Flask, render_template, url_for, redirect, request from authlib.integrations.flask_client import OAuth from secrets import token_urlsafe from requests import post app = Flask(__name__) SECRET_KEY = token_urlsafe(32) app.secret_key = SECRET_KEY oauth = OAuth(app) gotosocial = oauth.register( name='gotosocial', client_id='', client_secret='', access_token_url='/oauth/token', access_token_params={'response_type':'token', 'grant_type':'authorization_code', 'client_id':'', 'client_secret':''}, authorize_url='/oauth/authorize', authorize_params={'grant_type':'authorization_code'}, api_base_url='/api', client_kwargs={'scope': 'read'}, ) @app.route('/') def index(): return render_template('index.html') @app.route('/set_domain', methods=['POST']) def set_domain(): if(request.method == 'POST'): payload = { 'client_name':'gotosocial-fe', 'redirect_uris':url_for('index', _external=True) } response = post(f'https://{request.form["domain"]}/api/v1/apps', data=payload) client_data = response.json() gotosocial.client_id = client_data['client_id'] gotosocial.client_secret = client_data['client_secret'] gotosocial.access_token_params = { 'response_type':'token', 'grant_type':'authorization_code', 'client_id':client_data['client_id'], 'client_secret':client_data['client_secret'] } gotosocial.access_token_url = f'https://{request.form["domain"]}{gotosocial.access_token_url}' gotosocial.authorize_url = f'https://{request.form["domain"]}{gotosocial.authorize_url}' gotosocial.api_base_url = f'https://{request.form["domain"]}{gotosocial.api_base_url}' return redirect('/login') else: return "Sorry, but you can't get *GET* /set_domain, hun." @app.route('/login') def login(): redirect_uri = url_for('authorize', _external=True) return gotosocial.authorize_redirect(redirect_uri) @app.route('/authorize') def authorize(): token = gotosocial.authorize_access_token() print(token) response = gotosocial.get( 'api/v1/accounts/verify_credentials', token=token) response.raise_for_status() account_info = response.json() return account_info