Django-oauth-toolkit: Create access token directly

Created on 11 Mar 2016  路  5Comments  路  Source: jazzband/django-oauth-toolkit

Is it possible to create access token directly from my View?
I've written API method for user registration and I want to create the Access Token (with a Refresh Token ability) immediately after registration. How to do this corectly?

question

Most helpful comment

I've wrote the following code:

application = Application.objects.get(client_id=validated_data['client_id'])
expires = timezone.now() + timedelta(seconds=oauth2_settings.ACCESS_TOKEN_EXPIRE_SECONDS)
access_token = AccessToken(
    user=activated_user,
    scope='',
    expires=expires,
    token=common.generate_token(),
    application=application
)
access_token.save()
refresh_token = RefreshToken(
    user=activated_user,
    token=common.generate_token(),
    application=application,
    access_token=access_token
)
refresh_token.save()

But I'm not sure that it is correctly.

All 5 comments

I've wrote the following code:

application = Application.objects.get(client_id=validated_data['client_id'])
expires = timezone.now() + timedelta(seconds=oauth2_settings.ACCESS_TOKEN_EXPIRE_SECONDS)
access_token = AccessToken(
    user=activated_user,
    scope='',
    expires=expires,
    token=common.generate_token(),
    application=application
)
access_token.save()
refresh_token = RefreshToken(
    user=activated_user,
    token=common.generate_token(),
    application=application,
    access_token=access_token
)
refresh_token.save()

But I'm not sure that it is correctly.

Looks correct.

@EmilTemirov can you provide the full code. How do i get the common.generate_token() ??

@prabin525 In case anyone else wants to know - common refers to oauthlib.common, which is from the OAuthLib library that Django OAuth Toolkit extends.

@EmilTemirov can you provide the full code. How do i get the common.generate_token() ??

from oauthlib import common

common.generate_token()
...
Was this page helpful?
0 / 5 - 0 ratings