From 432de9fc3c1878ce4c2f6c6a9d2f3c9873a796d1 Mon Sep 17 00:00:00 2001 From: Paul Woolcock Date: Tue, 13 Feb 2018 00:22:13 -0500 Subject: [PATCH] Add the ability to use the browser auth flow --- fediplay.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/fediplay.py b/fediplay.py index 24bbac1..7359235 100644 --- a/fediplay.py +++ b/fediplay.py @@ -79,6 +79,12 @@ def login(api_base_url, email, password): client.log_in(email, password, to_file='usercred.secret') umask(old_umask) +def login_with_code(api_base_url, grant_code): + client = Mastodon(client_id='clientcred.secret', api_base_url=api_base_url) + old_umask = umask(0o77) + client.log_in(code=grant_code, to_file='usercred.secret') + umask(old_umask) + def stream(api_base_url): client = Mastodon(client_id='clientcred.secret', access_token='usercred.secret', api_base_url=api_base_url) listener = StreamListener() @@ -111,7 +117,7 @@ def build_play_command(filename): def main(): from getpass import getpass from os import path - from sys import exit + from sys import exit, argv api_base_url = environ.get('FEDIPLAY_API_BASE_URL') if not api_base_url: @@ -124,9 +130,16 @@ def main(): if not path.exists('usercred.secret'): print('==> No usercred.secret; logging in') - email = input('Email: ') - password = getpass('Password: ') - login(api_base_url, email, password) + if '-b' in argv or '--browser' in argv: + client = Mastodon(client_id='clientcred.secret', api_base_url=api_base_url) + print("Open this page in your browser and follow the instructions to get the code you need, then paste it here") + print(client.auth_request_url()) + grant_code = getpass('Code? ') + login_with_code(api_base_url, grant_code) + else: + email = input('Email: ') + password = getpass('Password: ') + login(api_base_url, email, password) stream(api_base_url)