diff options
author | Ansem <[email protected]> | 2017-04-10 15:23:08 +0000 |
---|---|---|
committer | Lorenz Diener <[email protected]> | 2017-04-11 10:36:58 +0200 |
commit | e51e5cc52192da40c891b28d54a60f03c5763e97 (patch) | |
tree | bdd67f7d533b482169684e802deaa87b659deaf1 | |
parent | ccaaea00724aeee0c449cb81a23c17c03c5e4545 (diff) | |
download | mastodon.py-e51e5cc52192da40c891b28d54a60f03c5763e97.tar.gz |
Fix auth request, now able to specify needed scope for the client
-rw-r--r-- | mastodon/Mastodon.py | 21 |
1 files changed, 14 insertions, 7 deletions
diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index de14ea1..98a371e 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py | |||
@@ -2,7 +2,6 @@ | |||
2 | 2 | ||
3 | 3 | ||
4 | import os | 4 | import os |
5 | from urllib.parse import urlencode | ||
6 | import os.path | 5 | import os.path |
7 | import mimetypes | 6 | import mimetypes |
8 | import time | 7 | import time |
@@ -10,10 +9,13 @@ import random | |||
10 | import string | 9 | import string |
11 | import pytz | 10 | import pytz |
12 | import datetime | 11 | import datetime |
13 | import dateutil | ||
14 | import dateutil.parser | ||
15 | from contextlib import closing | 12 | from contextlib import closing |
13 | from urllib.parse import urlencode | ||
14 | |||
15 | import pytz | ||
16 | import requests | 16 | import requests |
17 | import dateutil | ||
18 | import dateutil.parser | ||
17 | 19 | ||
18 | class Mastodon: | 20 | class Mastodon: |
19 | """ | 21 | """ |
@@ -48,6 +50,7 @@ class Mastodon: | |||
48 | 50 | ||
49 | Returns client_id and client_secret. | 51 | Returns client_id and client_secret. |
50 | """ | 52 | """ |
53 | |||
51 | request_data = { | 54 | request_data = { |
52 | 'client_name': client_name, | 55 | 'client_name': client_name, |
53 | 'scopes': " ".join(scopes) | 56 | 'scopes': " ".join(scopes) |
@@ -154,9 +157,9 @@ class Mastodon: | |||
154 | self._refresh_token = value | 157 | self._refresh_token = value |
155 | return | 158 | return |
156 | 159 | ||
157 | def auth_request_url(self, client_id: str = None, redirect_uris: str = "urn:ietf:wg:oauth:2.0:oob") -> str: | 160 | 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: |
158 | """Returns the url that a client needs to request the grant from the server. | 161 | """Returns the url that a client needs to request the grant from the server. |
159 | https://mastodon.social/oauth/authorize?client_id=XXX&response_type=code&redirect_uris=YYY | 162 | https://mastodon.social/oauth/authorize?client_id=XXX&response_type=code&redirect_uris=YYY&scope=read+write+follow |
160 | """ | 163 | """ |
161 | if client_id is None: | 164 | if client_id is None: |
162 | client_id = self.client_id | 165 | client_id = self.client_id |
@@ -169,6 +172,7 @@ class Mastodon: | |||
169 | params['client_id'] = client_id | 172 | params['client_id'] = client_id |
170 | params['response_type'] = "code" | 173 | params['response_type'] = "code" |
171 | params['redirect_uri'] = redirect_uris | 174 | params['redirect_uri'] = redirect_uris |
175 | params['scope'] = " ".join(scopes) | ||
172 | formatted_params = urlencode(params) | 176 | formatted_params = urlencode(params) |
173 | return "".join([self.api_base_url, "/oauth/authorize?", formatted_params]) | 177 | return "".join([self.api_base_url, "/oauth/authorize?", formatted_params]) |
174 | 178 | ||
@@ -203,7 +207,7 @@ class Mastodon: | |||
203 | params = self.__generate_params(locals(), ['scopes', 'to_file', 'username', 'password', 'code']) | 207 | params = self.__generate_params(locals(), ['scopes', 'to_file', 'username', 'password', 'code']) |
204 | params['grant_type'] = 'refresh_token' | 208 | params['grant_type'] = 'refresh_token' |
205 | else: | 209 | else: |
206 | raise MastodonIllegalArgumentError('Invalid user name, password, redirect_uris or scopes') | 210 | raise MastodonIllegalArgumentError('Invalid arguments given. username and password or code are required.') |
207 | 211 | ||
208 | params['client_id'] = self.client_id | 212 | params['client_id'] = self.client_id |
209 | params['client_secret'] = self.client_secret | 213 | params['client_secret'] = self.client_secret |
@@ -216,7 +220,10 @@ class Mastodon: | |||
216 | except Exception as e: | 220 | except Exception as e: |
217 | import traceback | 221 | import traceback |
218 | traceback.print_exc() | 222 | traceback.print_exc() |
219 | raise MastodonIllegalArgumentError('Invalid user name, password, redirect_uris or scopes: %s' % e) | 223 | if username is not None or password is not None: |
224 | raise MastodonIllegalArgumentError('Invalid user name, password, or redirect_uris: %s' % e) | ||
225 | elif code is not None: | ||
226 | raise MastodonIllegalArgumentError('Invalid access token or redirect_uris: %s' % e) | ||
220 | 227 | ||
221 | requested_scopes = " ".join(sorted(scopes)) | 228 | requested_scopes = " ".join(sorted(scopes)) |
222 | received_scopes = " ".join(sorted(response["scope"].split(" "))) | 229 | received_scopes = " ".join(sorted(response["scope"].split(" "))) |