Skip to content

Instantly share code, notes, and snippets.

@davestgermain
Created October 19, 2018 14:24
Show Gist options
  • Save davestgermain/9909a67034a037f9cb32532d8079e48f to your computer and use it in GitHub Desktop.
Save davestgermain/9909a67034a037f9cb32532d8079e48f to your computer and use it in GitHub Desktop.
from django.apps import apps
from django.db.utils import IntegrityError
def create_oauth_credentials(name):
"""
Automatically create oauth credentials for applications
"""
username = 'oauth_api_%s' % name
try:
auth_user = get_user_model().objects.create_user(username)
except IntegrityError:
auth_user = get_user_model().objects.get(username=username)
app, created = apps.get_model('oauth2_provider.Application').objects.get_or_create(
name='Oauth User %s' % name,
user=auth_user,
client_type='confidential',
authorization_grant_type='client-credentials'
)
return app.client_id, app.client_secret, username, created
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment