Skip to content
This repository has been archived by the owner on Sep 27, 2022. It is now read-only.

dt.strftime('%S') #3

Open
srdavicho opened this issue Apr 19, 2017 · 10 comments
Open

dt.strftime('%S') #3

srdavicho opened this issue Apr 19, 2017 · 10 comments

Comments

@srdavicho
Copy link

I could runserver following hunterway55 comments: upgrading to django=1.10.5 and adding django-cors-headers==1.3.1, but the Postman register & login commands didn't work.

In apps/authentication/models.py

I replaced dt.strftime('%s') by dt.strftime('%S') and the Login & Register commands worked.

Token generation function is now:

def _generate_jwt_token(self):
    """
    Generates a JSON Web Token that stores this user's ID and has an expiry
    date set to 60 days into the future.
    """
    dt = datetime.now() + timedelta(days=60)

    token = jwt.encode({
        'id': self.pk,
        'exp': int(dt.strftime('%S'))
    }, settings.SECRET_KEY, algorithm='HS256')

    return token.decode('utf-8')

Hope it helps someone.

@jamesbrewerdev
Copy link
Contributor

What version of Python are you using?

@srdavicho
Copy link
Author

Python 3.5.1 on windows
Pip freeze:
appdirs==1.4.3
Django==1.10.5
django-cors-headers==1.3.1
django-cors-middleware==1.2.0
django-extensions==1.7.1
djangorestframework==3.4.4
packaging==16.8
PyJWT==1.4.2
pyparsing==2.2.0
six==1.10.0

I'm also currently doing the tutorial on Thinkster, and the upper change worked for the Login and Register Postman commands only, but did not work for Current User and Update User commands. I fixed the issue by making the following change:

#dt = datetime.now() + timedelta(days=60)

    token = jwt.encode({
        'id': self.pk,
        'exp': datetime.now()+ timedelta(days=60)
    }, settings.SECRET_KEY, algorithm='HS256')

    return token.decode('utf-8')

@dissdoc
Copy link

dissdoc commented May 7, 2017

In my mind, you use Windows. I fix this problem like this
'exp': str(time.mktime(dt.timetuple()))[:-2]

@mkhan935
Copy link

same for me, I had to do %S for it work, maybe create a note in the actual code snippet

@dissdoc
Copy link

dissdoc commented May 13, 2017

%S is not %s! It's wrong!

@mkhan935
Copy link

%S signifies seconds yes? what does %s mean?

@srdavicho
Copy link
Author

Accorting to http://strftime.org/

%S means Second as a zero-padded decimal number. Example: 05

@rcdosado
Copy link

rcdosado commented Sep 9, 2017

thanks for this, but still the fix is not yet committed in the repo. as of now.

@Nifled
Copy link

Nifled commented Oct 2, 2017

@dissdoc is right, %S is definitely not the same as %s. I've created a PR for a compatible solution for anyone still using this as a reference like I am.

@cgitosh
Copy link

cgitosh commented Sep 6, 2021

Are there any updates to this? Followed this tutorial https://thinkster.io/tutorials/django-json-api/authentication and I'm getting error after error

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants