aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnsem <[email protected]>2017-04-10 15:23:08 +0000
committerLorenz Diener <[email protected]>2017-04-11 10:36:58 +0200
commite51e5cc52192da40c891b28d54a60f03c5763e97 (patch)
treebdd67f7d533b482169684e802deaa87b659deaf1 /mastodon
parentccaaea00724aeee0c449cb81a23c17c03c5e4545 (diff)
downloadmastodon.py-e51e5cc52192da40c891b28d54a60f03c5763e97.tar.gz
Fix auth request, now able to specify needed scope for the client
Diffstat (limited to 'mastodon')
-rw-r--r--mastodon/Mastodon.py21
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
4import os 4import os
5from urllib.parse import urlencode
6import os.path 5import os.path
7import mimetypes 6import mimetypes
8import time 7import time
@@ -10,10 +9,13 @@ import random
10import string 9import string
11import pytz 10import pytz
12import datetime 11import datetime
13import dateutil
14import dateutil.parser
15from contextlib import closing 12from contextlib import closing
13from urllib.parse import urlencode
14
15import pytz
16import requests 16import requests
17import dateutil
18import dateutil.parser
17 19
18class Mastodon: 20class 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(" ")))
Powered by cgit v1.2.3 (git 2.41.0)