Last updated
Last updated
You can create and manage OAuth applications at .
With the Django OAuth Toolkit, Ion supports accessing API and other resources via . This allows for applications to be written using the Ion API without the need to prompt for user credentials from within the application. Instead, access tokens are used to gain access to Ion API resources. This functionality enables you to have users authenticate to your website using their Ion account. This is especially useful for apps that are meant to serve the TJ community.
Go into on Ion and click Create Application
. Specify the following values in the form:
Name
Some descriptive name for your application.
Client Type
Choose Confidential
if your app has a backend component and your server can store the client ID and secret securely.
Choose Public
if your app is purely client-side and a copy of the credentials will be distributed publicly.
Authorization Grant Type
Choose Authorization code
if your client type is Confidential
.
Choose Implicit
if your client type is Public
.
Redirect URIs
Enter one or more URLs that your application will redirect back to after the authorization is complete.
Algorithm
If this option exists, leave it at the default No OIDC support
. Ion doesn't support OpenID.
For a Python client, use requests
with requests-oauthlib
. If running locally (without HTTPS), override the SSL requirement for OAuth2.
Create an OAuth2Session, with the CLIENT_ID
and REDIRECT_URI
you entered in the application form. Redirect the user to authorization_url
.
The user authenticates, approves the request, and is redirected to the callback URL specified in redirect_uri
, with a "code" GET parameter.
At this point, a valid access token has been gained, and you can request API resources.
After 36,000 seconds (1 hour), the token will expire; you need to renew it. This can be handled by putting API commands inside a try-except for a oauthlib.oauth2.TokenExpiredError
, such as seen above. Alternatively, you can provide auto_refresh_url=refresh_url, auto_refresh_kwargs=args
as additional arguments to OAuth2Session when it is created.
Note: This code will not work out of the box. Read the comments carefully to determine how to integrate it into your application.
Inside your application, redirect to the OAuth authorization endpoint to receive an authorization code. The url is . To access the API, exchange this code for a (temporary) access token. The URL is .
If you want to use python-social-auth
, a plugin is available in the ion_oauth package. You can get an older version or download an updated version directly from GitHub .
For a Django project add AUTHENTICATION_BACKENDS = ['ion_oauth.oauth.IonOauth2']
and define SOCIAL_AUTH_ION_KEY
and SOCIAL_AUTH_ION_SECRET
in your settings.py
file. The redirect_uri
s for Django projects should be "http://<site-url>/complete/ion/" and "http://<site-url>/complete/ion".
You can use the library to perform authentication. Below is some sample code.
The ultimate guide to OAuth authentication through Ion.