Sample Django App using Twitter Sign in (OAuth) and REST APIs. This is the basis for many types of campaigns, including:
As always, when developing on top of the Twitter platform, you must abide by the Developer Agreement & Policy.
To run this sample code, you'll need to install the following libraries:
You can install these with the following command:
sudo pip install -r requirements.txt
Create a Twitter App (https://apps.twitter.com/)
In the Twitter App config, ensure the Callback URL is
In the settings.py or settings_my.py, set the following to your own key or set the secret key as an environment variable in the production environment:
SECRET_KEY = 'YOUR_SECRET_KEY'
Specify your Twitter API credentials in app/settings_my.py under the following section:
SOCIAL_AUTH_TWITTER_KEY = ''
SOCIAL_AUTH_TWITTER_SECRET = ''
TWITTER_ACCESS_TOKEN = ''
TWITTER_ACCESS_TOKEN_SECRET = ''
To initialize your database, run the from the
python manage.py makemigrations home --settings=app.settings_my
python manage.py migrate --settings=app.settings_my
Create an admin user for the Django admin by running the following:
python manage.py createsuperuser --settings=app.settings_my
To start the server, run the following from the
Open a browser and go to http://127.0.0.1:9000
This code sample allows for restriction of Django admin based on IP. To enable this, do the following:
Run the migration for this as follows:
python manage.py makemigrations adminrestrict --settings=app.settings_my
`python manage.py migrate adminrestrict --settings=app.settings_my
Log in as the Admin user and create a * record in the adminrestrict.allowed_ip table.
Lastly, uncomment the following from the MIDDLEWARE_CLASSES section in settings.py:
For security, this code sample has a batch process to clear out Twitter auth tokens for users that either:
To run the process, simply execute:
Deploying to Heroku is even easier. The defaults in settings.py are pre-configured to easily deploy to Heroku.
Create a Twitter App (https://apps.twitter.com/)
Generate the Twitter API credentials and enter the proper values for "value" in the app.json file
Click on the Deploy Heroku button below
When prompted during the Heroku install, specify your credentials if they were not already specified in the app.json file:
After deploying, in the Twitter App config, ensure the Callback URL is
On the Heroku Dashboard for your app, click on the Deploy tab and connect the app to the proper Github repo
To sync the database, use the Heroku CLI and run the following:
heroku run python manage.py migrate --app your-app-name
Open a browser and go to the URL specified by your deploy (http://your-app-name.herokuapp.com)
To create an admin user, use the following Heroku CLI command:
heroku run python manage.py createsuperuser --username=USERNAME --email=EMAIL --app your-app-name
Then log in via the Admin console and update your initial Twitter login user accordingly.
To ensure the token invalidation script works properly on Heroku, run the following from your machine:
`heroku run fab invalidate --app=MY_APP_NAME'
If this runs properly, follow the below steps to run it as a scheduled job on Heroku:
heroku addons:create scheduler:standard --app MY_APP_NAME
Confirm successful execution by viewing the output in the Heroku app logs.
This sample code also allows you to upload and inspect video. It requires that ffprobe (as part of ffmpeg) be installed on the machine. When installed, video uploads will show additional debugging information:
By default, the media debug is included on the publicly maintained version of this site: http://django-rest-apis.herokuapp.com. When posting video issues to the Twitter Community forums, please include the output of the Media Inspector to help us debug/investigate issues.
To enable the ffmpeg on your local machine, visit the ffprobe/ffmpeg documentation.
Additionally, you can get the ffprobe portion of the code sample working on Heroku as well. You need to do the following:
Set the root buildpack with the following setting:
heroku buildpacks:set https://github.com/integricho/heroku-buildpack-python-ffmpeg --app your-app-name
Which will tell you to rebuild, as such:
Buildpack set. Next release on hellobootcamp will use https://github.com/integricho/heroku-buildpack-python-ffmpeg. Run git push heroku master to create a new release using this buildpack.
If you receive a 401 at login/twitter it is most likely caused by a datetime discrepancy between the server making the requst and the Twitter server.
Use NTP to sync time on your server to compensate for the dift.
If you are getting this error on OSX, toggle the "set time zone" checkbox off and back on in Date & Time system preferences for a manual and temporary fix. It has been reported that OSX 10.9 Mavericks has an issue with time drift.
If you are still receiving the 401 error, it could be due to invalid credentials. Be sure to double check the DJANGO_SECRET_KEY, the Access Token value, the Acess Token Secret value, Consumer Key value, and Consumer Secret value in all the places they are specified.
Lastly, sample video provided by TechSlides.com.