Package: simple-django-twitter-auth
# Simple Django Twitter AuthenticationSimple Django Twitter Authentication is an explicit, simple way to add “Login with Twitter” functionality to your Django project. The goal is to keep control in your hands, and to allow customization and implementation without having to dig through pages of documentation.## Installationpip install simple-django-twitter-authGo to [https://apps.twitter.com](https://apps.twitter.com) and register an app if you haven’t alreadyMake sure the “Allow this application to be used to Sign in with Twitter” option is enabled in your “settings” page!Grab yourAPI keyandAPI secretfrom your Twitter app dashboardDefine the following settings insettings.pyTWITTER_APP_KEY = ‘myappkey’TWITTER_APP_SECRET = ‘myappsecret’It’s recommended you use environment variables instead of defining confidential credentials in your settings fileDefineHOSTinsettings.pyHOST = ‘https://3efed1b4.ngrok.com’Don’t include a trailing backslashGOOD:https://3efed1b4.ngrok.comBAD:https://3efed1b4.ngrok.com/Add ‘django_twitter_auth’ to yourINSTALLED_APPSinsettings.py.Add the following line to the top of your rooturls.pyurl(r’^twitter/’, include(‘django_twitter_auth.urls’, namespace=’django_twitter_auth’)),Run migrate to install theTwitterProfilemodel that comes with Simple Django Twitter Authpython manage.py migrateThat’s it! You can now use the{% url ‘django_twitter_auth:login’ %}template tag to kick off the login flow.## Login FlowSimple Django Twitter Auth provides the following URL/twitter/login/. First, you point users here.you can also utilize the{% url ‘django_twitter_auth:login’ %}template tagUsers are redirected to Twitter where they authorize your application, granting it access to their Twitter profile.After authorizing your app, Twitter redirects users back to your site. Simple Django Twitter Auth then does one of the following:Creates a newTwitterProfileandUserFinds an existingTwitterProfileandUserFinds an existingTwitterProfileandUser, and updates theTwitterProfile’s OAuth2 tokens.(if a Twitter user revoked access to your app, and then re-authorizes it later, Simple Django Twitter Auth simply updates the access tokens.)Simple Django Twitter Auth manually logs in the user, and redirects them back to the page they started the flow from.## Components#### TwitterProfileSimple Django Twitter Auth provides aTwitterProfilemodel. This model has the following attributes:TwitterProfile.OAUTH_TOKENOAuth2 token provided by Twitter during authorization. Can be used to consume/publish additional data on behalf of Twitter User.TwitterProfile.OAUTH_TOKEN_SECRETOAuth2 token secret provided by Twitter during authorization. Can be used to consume/publish additional data on behalf of Twitter User.TwitterProfile.usernameUser’s Twitter username. ‘@’ not included.TwitterProfile.userWhenever a newTwitterProfileis created, aUseris also created and a OneToOne relationship is established with theTwitterProfile.Theusernameattribute of theUseris set to the same value asTwitterProfile.username.Simple Django Twitter Auth usesdjango.contrib.auth.get_user_model()to get the currentUsermodel.Reverse lookup is available throughuser.twitterprofile.## Customization#### TWITTER_NEW_USER_URLDefine a location for new users to be redirected to. Eg:TWITTER_NEW_USER_URL = “/welcome/”Users are logged in by the time they arrive here.If this setting is defined, Simple Django Twitter Auth will append aredir_toURL parameter when forwarding. This will contain the URL the user started the login flow from. For example,?redir_to=/welcome/.You can catch this parameter to redirect the user back to where they started after you’re done any custom logic defined in yourTWITTER_NEW_USER_URLview.#### TWITTER_AUTH_RANDOM_PASSWORDDefault isTrue.When Simple Django Twitter Auth creates aUserand ties it to theTwitterProfile, a random base64 encoded 128-bit password usingos.urandom()is created for theUser. This is just a protective measure, so that theUserisn’t created with a blank password.You can turn this off if you want, by settingTWITTER_AUTH_RANDOM_PASSWORDtoFalse.## Running TestsCan manually run tests by callingmanage.py test django_twitter_auth## Coming SoonSignals to provide additional customizationPictures in the readmeBetter exampleMore coming soon…