aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLorenz Diener <[email protected]>2016-11-24 12:03:42 +0100
committerLorenz Diener <[email protected]>2016-11-24 12:03:42 +0100
commit68899a0cc4837f0202e2542f5308e61d079e7a2e (patch)
treeb41ffa0d1eae628eef0ffffaccdd6579f08e1502
parent3a92948158c28d0fe14498bdb90d8259ff8a8345 (diff)
downloadmastodon.py-68899a0cc4837f0202e2542f5308e61d079e7a2e.tar.gz
Added some error handling to log_in
-rw-r--r--mastodon/Mastodon.py20
1 files changed, 17 insertions, 3 deletions
diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py
index 558d1c1..efbe89d 100644
--- a/mastodon/Mastodon.py
+++ b/mastodon/Mastodon.py
@@ -95,8 +95,17 @@ class Mastodon:
95 params['grant_type'] = 'password' 95 params['grant_type'] = 'password'
96 params['scope'] = " ".join(scopes) 96 params['scope'] = " ".join(scopes)
97 97
98 response = self.__api_request('POST', '/oauth/token', params) 98 try:
99 self.access_token = response['access_token'] 99 response = self.__api_request('POST', '/oauth/token', params)
100 self.access_token = response['access_token']
101 except:
102 raise ValueError('Invalid user name, password or scopes.')
103
104 requested_scopes = " ".join(scopes)
105 received_scopes = " ".join(sorted(response["scopes"].split(" ")))
106
107 if requested_scopes != received_scopes:
108 raise ValueError('Granted scopes "' + received_scopes + '" differ from requested scopes "' + requested_scopes + '".')
100 109
101 if to_file != None: 110 if to_file != None:
102 with open(to_file, 'w') as token_file: 111 with open(to_file, 'w') as token_file:
@@ -330,7 +339,12 @@ class Mastodon:
330 if response.status_code == 500: 339 if response.status_code == 500:
331 raise IOError('General API problem.') 340 raise IOError('General API problem.')
332 341
333 return response.json() 342 try:
343 response = response.json()
344 except:
345 raise ValueError("Could not parse response as JSON, respose code was " + str(response.status_code))
346
347 return response
334 348
335 def __generate_params(self, params, exclude = []): 349 def __generate_params(self, params, exclude = []):
336 """ 350 """
Powered by cgit v1.2.3 (git 2.41.0)