aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLorenz Diener <[email protected]>2019-04-28 01:02:08 +0200
committerLorenz Diener <[email protected]>2019-04-28 01:02:08 +0200
commit0ffb869e6565c25bc1237379237e55ae24872b23 (patch)
tree7a9c48c7c47feb1c4a6947511c07e63143933fab /mastodon/Mastodon.py
parent429132e9566f33f7f076705b65636dc517060b59 (diff)
downloadmastodon.py-0ffb869e6565c25bc1237379237e55ae24872b23.tar.gz
Improve OAuth support and docs
Diffstat (limited to 'mastodon/Mastodon.py')
-rw-r--r--mastodon/Mastodon.py34
1 files changed, 28 insertions, 6 deletions
diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py
index abd6b42..5ba33d4 100644
--- a/mastodon/Mastodon.py
+++ b/mastodon/Mastodon.py
@@ -190,14 +190,19 @@ class Mastodon:
190 Create a new app with given `client_name` and `scopes` (The basic scropse are "read", "write", "follow" and "push" 190 Create a new app with given `client_name` and `scopes` (The basic scropse are "read", "write", "follow" and "push"
191 - more granular scopes are available, please refere to Mastodon documentation for which). 191 - more granular scopes are available, please refere to Mastodon documentation for which).
192 192
193 Specify `redirect_uris` if you want users to be redirected to a certain page after authenticating. 193 Specify `redirect_uris` if you want users to be redirected to a certain page after authenticating in an oauth flow.
194 You can specify multiple URLs by passing a list. Note that if you wish to use OAuth authentication with redirects,
195 the redirect URI must be one of the URLs specified here.
196
194 Specify `to_file` to persist your apps info to a file so you can use them in the constructor. 197 Specify `to_file` to persist your apps info to a file so you can use them in the constructor.
195 Specify `api_base_url` if you want to register an app on an instance different from the flagship one. 198 Specify `api_base_url` if you want to register an app on an instance different from the flagship one.
199 Specify `website` to give a website for your app.
196 200
197 Specify `session` with a requests.Session for it to be used instead of the deafult. 201 Specify `session` with a requests.Session for it to be used instead of the deafult.
198 202
199 Presently, app registration is open by default, but this is not guaranteed to be the case for all 203 Presently, app registration is open by default, but this is not guaranteed to be the case for all
200 future mastodon instances or even the flagship instance in the future. 204 future mastodon instances or even the flagship instance in the future.
205
201 206
202 Returns `client_id` and `client_secret`, both as strings. 207 Returns `client_id` and `client_secret`, both as strings.
203 """ 208 """
@@ -210,6 +215,8 @@ class Mastodon:
210 215
211 try: 216 try:
212 if redirect_uris is not None: 217 if redirect_uris is not None:
218 if isinstance(redirect_uris, (list, tuple)):
219 redirect_uris = "\n".join(list(redirect_uris))
213 request_data['redirect_uris'] = redirect_uris 220 request_data['redirect_uris'] = redirect_uris
214 else: 221 else:
215 request_data['redirect_uris'] = 'urn:ietf:wg:oauth:2.0:oob' 222 request_data['redirect_uris'] = 'urn:ietf:wg:oauth:2.0:oob'
@@ -370,8 +377,20 @@ class Mastodon:
370 return Mastodon.__SUPPORTED_MASTODON_VERSION 377 return Mastodon.__SUPPORTED_MASTODON_VERSION
371 378
372 def auth_request_url(self, client_id=None, redirect_uris="urn:ietf:wg:oauth:2.0:oob", 379 def auth_request_url(self, client_id=None, redirect_uris="urn:ietf:wg:oauth:2.0:oob",
373 scopes=__DEFAULT_SCOPES): 380 scopes=__DEFAULT_SCOPES, force_login=False):
374 """Returns the url that a client needs to request the grant from the server. 381 """
382 Returns the url that a client needs to request an oauth grant from the server.
383
384 To log in with oauth, send your user to this URL. The user will then log in and
385 get a code which you can pass to log_in.
386
387 scopes are as in `log_in()`_, redirect_uris is where the user should be redirected to
388 after authentication. Note that redirect_uris must be one of the URLs given during
389 app registration. When using urn:ietf:wg:oauth:2.0:oob, the code is simply displayed,
390 otherwise it is added to the given URL as the "code" request parameter.
391
392 Pass force_login if you want the user to always log in even when already logged
393 into web mastodon (i.e. when registering multiple different accounts in an app).
375 """ 394 """
376 if client_id is None: 395 if client_id is None:
377 client_id = self.client_id 396 client_id = self.client_id
@@ -385,6 +404,7 @@ class Mastodon:
385 params['response_type'] = "code" 404 params['response_type'] = "code"
386 params['redirect_uri'] = redirect_uris 405 params['redirect_uri'] = redirect_uris
387 params['scope'] = " ".join(scopes) 406 params['scope'] = " ".join(scopes)
407 params['force_login'] = force_login
388 formatted_params = urlencode(params) 408 formatted_params = urlencode(params)
389 return "".join([self.api_base_url, "/oauth/authorize?", formatted_params]) 409 return "".join([self.api_base_url, "/oauth/authorize?", formatted_params])
390 410
@@ -404,8 +424,10 @@ class Mastodon:
404 username / password credentials given are incorrect, and 424 username / password credentials given are incorrect, and
405 `MastodonAPIError` if all of the requested scopes were not granted. 425 `MastodonAPIError` if all of the requested scopes were not granted.
406 426
407 For OAuth2 documentation, compare 427 For OAuth2, obtain a code via having your user go to the url returned by
408 https://github.com/doorkeeper-gem/doorkeeper/wiki/Interacting-as-an-OAuth-client-with-Doorkeeper 428 `auth_request_url()`_ and pass it as the code parameter. In this case,
429 make sure to also pass the same redirect_uri parameter as you used when
430 generating the auth request URL.
409 431
410 Returns the access token as a string. 432 Returns the access token as a string.
411 """ 433 """
Powered by cgit v1.2.3 (git 2.41.0)