From 0d251bbf462ffafe93fa23606728abb7759ce7fa Mon Sep 17 00:00:00 2001 From: Ansem Date: Mon, 10 Apr 2017 15:23:08 +0000 Subject: [PATCH 1/2] Fix auth request, now able to specify needed scope for the client --- mastodon/Mastodon.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index ee11496..9a77749 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py @@ -2,17 +2,19 @@ import os -from urllib.parse import urlencode import os.path import mimetypes import time import random import string import datetime +from contextlib import closing +from urllib.parse import urlencode + +import pytz +import requests import dateutil import dateutil.parser -from contextlib import closing -import requests class Mastodon: """ @@ -47,6 +49,7 @@ class Mastodon: Returns client_id and client_secret. """ + request_data = { 'client_name': client_name, 'scopes': " ".join(scopes) @@ -153,9 +156,9 @@ class Mastodon: self._refresh_token = value return - def auth_request_url(self, client_id: str = None, redirect_uris: str = "urn:ietf:wg:oauth:2.0:oob") -> str: + def auth_request_url(self, client_id: str = None, redirect_uris: str = "urn:ietf:wg:oauth:2.0:oob", scopes: list = ['read', 'write', 'follow']) -> str: """Returns the url that a client needs to request the grant from the server. - https://mastodon.social/oauth/authorize?client_id=XXX&response_type=code&redirect_uris=YYY + https://mastodon.social/oauth/authorize?client_id=XXX&response_type=code&redirect_uris=YYY&scope=read+write+follow """ if client_id is None: client_id = self.client_id @@ -168,6 +171,7 @@ class Mastodon: params['client_id'] = client_id params['response_type'] = "code" params['redirect_uri'] = redirect_uris + params['scope'] = " ".join(scopes) formatted_params = urlencode(params) return "".join([self.api_base_url, "/oauth/authorize?", formatted_params]) @@ -202,7 +206,7 @@ class Mastodon: params = self.__generate_params(locals(), ['scopes', 'to_file', 'username', 'password', 'code']) params['grant_type'] = 'refresh_token' else: - raise MastodonIllegalArgumentError('Invalid user name, password, redirect_uris or scopes') + raise MastodonIllegalArgumentError('Invalid arguments given. username and password or code are required.') params['client_id'] = self.client_id params['client_secret'] = self.client_secret @@ -215,7 +219,10 @@ class Mastodon: except Exception as e: import traceback traceback.print_exc() - raise MastodonIllegalArgumentError('Invalid user name, password, redirect_uris or scopes: %s' % e) + if username is not None or password is not None: + raise MastodonIllegalArgumentError('Invalid user name, password, or redirect_uris: %s' % e) + elif code is not None: + raise MastodonIllegalArgumentError('Invalid access token or redirect_uris: %s' % e) requested_scopes = " ".join(sorted(scopes)) received_scopes = " ".join(sorted(response["scope"].split(" "))) From 2ecac20890bf8dc0bdd5761db7a8bad9eb0c0647 Mon Sep 17 00:00:00 2001 From: Ansem Date: Mon, 10 Apr 2017 15:25:39 +0000 Subject: [PATCH 2/2] Add general error message --- mastodon/Mastodon.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index 9a77749..48e0e7a 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py @@ -223,6 +223,8 @@ class Mastodon: raise MastodonIllegalArgumentError('Invalid user name, password, or redirect_uris: %s' % e) elif code is not None: raise MastodonIllegalArgumentError('Invalid access token or redirect_uris: %s' % e) + else: + raise MastodonIllegalArgumentError('Invalid request: %s' % e) requested_scopes = " ".join(sorted(scopes)) received_scopes = " ".join(sorted(response["scope"].split(" ")))