aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLorenz Diener <[email protected]>2019-04-28 17:56:20 +0200
committerLorenz Diener <[email protected]>2019-04-28 17:56:20 +0200
commitb6692f0b16820da8388a5445bcfbf464a4648c91 (patch)
tree1facbe6e5755f8c3894048cda3bbb0ce9466fa20 /mastodon
parenta29d278bf9cacf5f888561564f112312707e32fd (diff)
downloadmastodon.py-b6692f0b16820da8388a5445bcfbf464a4648c91.tar.gz
Add account creation
Diffstat (limited to 'mastodon')
-rw-r--r--mastodon/Mastodon.py72
1 files changed, 69 insertions, 3 deletions
diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py
index b19c3de..444c12d 100644
--- a/mastodon/Mastodon.py
+++ b/mastodon/Mastodon.py
@@ -476,7 +476,71 @@ class Mastodon:
476 self.__logged_in_id = None 476 self.__logged_in_id = None
477 477
478 return response['access_token'] 478 return response['access_token']
479 479
480 @api_version("2.7.0", "2.7.0", "2.7.0")
481 def create_account(self, username, password, email, agreement=False, locale="en", scopes=__DEFAULT_SCOPES, to_file=None):
482 """
483 Creates a new user account with the given username, password and email. "agreement"
484 must be set to true (after showing the user the instances user agreement and having
485 them agree to it), "locale" specifies the language for the confirmation e-mail as an
486 ISO 639-1 (two-letter) language code.
487
488 Does not require an access token, but does require a client grant.
489
490 By default, this method is rate-limited by IP to 5 requests per 30 minutes.
491
492 Returns an access token (just like log_in), which it can also persist to to_file,
493 and sets it internally so that the user is now logged in. Note that this token
494 can only be used after the user has confirmed their e-mail.
495 """
496 params = self.__generate_params(locals(), ['to_file', 'scopes'])
497 params['client_id'] = self.client_id
498 params['client_secret'] = self.client_secret
499
500 if agreement == False:
501 del params_initial['agreement']
502
503 # Step 1: Get a user-free token via oauth
504 try:
505 oauth_params = {}
506 oauth_params['scope'] = " ".join(scopes)
507 oauth_params['client_id'] = self.client_id
508 oauth_params['client_secret'] = self.client_secret
509 oauth_params['grant_type'] = 'client_credentials'
510
511 response = self.__api_request('POST', '/oauth/token', oauth_params, do_ratelimiting=False)
512 temp_access_token = response['access_token']
513 except Exception as e:
514 raise MastodonIllegalArgumentError('Invalid request during oauth phase: %s' % e)
515
516 # Step 2: Use that to create a user
517 try:
518 response = self.__api_request('POST', '/api/v1/accounts', params, do_ratelimiting=False,
519 access_token_override = temp_access_token)
520 self.access_token = response['access_token']
521 self.__set_refresh_token(response.get('refresh_token'))
522 self.__set_token_expired(int(response.get('expires_in', 0)))
523 except Exception as e:
524 raise MastodonIllegalArgumentError('Invalid request: %s' % e)
525
526 # Step 3: Check scopes, persist, et cetera
527 received_scopes = response["scope"].split(" ")
528 for scope_set in self.__SCOPE_SETS.keys():
529 if scope_set in received_scopes:
530 received_scopes += self.__SCOPE_SETS[scope_set]
531
532 if not set(scopes) <= set(received_scopes):
533 raise MastodonAPIError(
534 'Granted scopes "' + " ".join(received_scopes) + '" do not contain all of the requested scopes "' + " ".join(scopes) + '".')
535
536 if to_file is not None:
537 with open(to_file, 'w') as token_file:
538 token_file.write(response['access_token'] + '\n')
539
540 self.__logged_in_id = None
541
542 return response['access_token']
543
480 ### 544 ###
481 # Reading data: Instances 545 # Reading data: Instances
482 ### 546 ###
@@ -2287,7 +2351,7 @@ class Mastodon:
2287 json_object = Mastodon.__json_allow_dict_attrs(json_object) 2351 json_object = Mastodon.__json_allow_dict_attrs(json_object)
2288 return json_object 2352 return json_object
2289 2353
2290 def __api_request(self, method, endpoint, params={}, files={}, headers={}, do_ratelimiting=True): 2354 def __api_request(self, method, endpoint, params={}, files={}, headers={}, access_token_override=None, do_ratelimiting=True):
2291 """ 2355 """
2292 Internal API request helper. 2356 Internal API request helper.
2293 """ 2357 """
@@ -2314,8 +2378,10 @@ class Mastodon:
2314 2378
2315 # Generate request headers 2379 # Generate request headers
2316 headers = copy.deepcopy(headers) 2380 headers = copy.deepcopy(headers)
2317 if self.access_token is not None: 2381 if not self.access_token is None:
2318 headers['Authorization'] = 'Bearer ' + self.access_token 2382 headers['Authorization'] = 'Bearer ' + self.access_token
2383 if not access_token_override is None:
2384 headers['Authorization'] = 'Bearer ' + access_token_override
2319 2385
2320 if self.debug_requests: 2386 if self.debug_requests:
2321 print('Mastodon: Request to endpoint "' + endpoint + '" using method "' + method + '".') 2387 print('Mastodon: Request to endpoint "' + endpoint + '" using method "' + method + '".')
Powered by cgit v1.2.3 (git 2.41.0)