wagnerdelima / drf-social-oauth2

drf-social-oauth2 makes it easy to integrate Django social authentication with major OAuth2 providers, i.e., Facebook, Twitter, Google, etc.
https://drf-social-oauth2.readthedocs.io/en/latest/
MIT License
274 stars 34 forks source link

TypeError: can't subtract offset-naive and offset-aware datetimes #108

Closed smithumble closed 2 years ago

smithumble commented 2 years ago

Got an error with django setting USE_TZ = False

  File ".../drf_social_oauth2/oauth2_endpoints.py", line 112, in create_token_response
    access_token.expires - datetime.now(tz=timezone.utc)
TypeError: can't subtract offset-naive and offset-aware datetimes

Related code snipet

        # if token is valid, do not create a new token, just return the token.
        token = {
            'access_token': access_token.token,
            'expires_in': (
                access_token.expires - datetime.now(tz=timezone.utc)
            ).total_seconds(),
            'scope': access_token.scope,
            'refresh_token': access_token.refresh_token.token,
            'token_type': 'Bearer',
        }
smithumble commented 2 years ago

Probably should use django timezone.now()

def now():
    """
    Return an aware or naive datetime.datetime, depending on settings.USE_TZ.
    """
    return datetime.now(tz=utc if settings.USE_TZ else None)

like:

            'expires_in': (
                access_token.expires - timezone.now()
            ).total_seconds(),
wagnerdelima commented 2 years ago

@smithumble thank you so much for your issue and PR. I promise I will test it myself later today. Perhaps even launch a new release tag.

wagnerdelima commented 2 years ago

@smithumble you can find the newest release with your changes here: https://pypi.org/project/drf-social-oauth2/. Thank you for your contribution.