diff --git a/Pipfile b/Pipfile index 77827ab..a5cd927 100644 --- a/Pipfile +++ b/Pipfile @@ -15,6 +15,7 @@ python_version = "3.6" "psycopg2-binary" = "*" django-heroku = "*" gunicorn = "*" +requests = "*" [dev-packages] diff --git a/README.md b/README.md index 7d59bc9..9d8a014 100644 --- a/README.md +++ b/README.md @@ -10,9 +10,11 @@ ## How to Use - Configure enviroment variables as follows: -``` +```bash DATABASE_URL=postgres://postgres:password@localhost:5432/ussd PLATFORM_URL=https://ussd.ushahidi.io +PLATFORM_URL=admin@ushahidi.com +PLATFORM_URL=************* ``` - Install dependencies using: `pipenv install` - Migrate DB using: `python manage.py migrate` diff --git a/ussd/views.py b/ussd/views.py index f428776..db03c75 100644 --- a/ussd/views.py +++ b/ussd/views.py @@ -1,8 +1,31 @@ import os +import json +import requests +import urllib.parse from django.shortcuts import render from django.http import HttpResponse PLATFORM_URL = os.environ.get('PLATFORM_URL', 'PLATFORM_URL NOT AVAILABLE') +EMAIL = os.environ.get('PLATFORM_EMAIL', '') +PASSWORD = os.environ.get('PLATFORM_PASSWORD', '') +AUTH_URL = urllib.parse.urljoin(PLATFORM_URL, '/oauth/token/') +ACCESS_TOKEN = None + +def get_access_token(): + payload = { + 'username': EMAIL, + 'password': PASSWORD, + 'client_id': 'ushahidiui', + 'client_secret': '35e7f0bca957836d05ca0492211b0ac707671261', + 'scope': '*', + 'grant_type': 'password' + } + headers = { + 'Content-Type': "application/json", + } + return requests.request('POST', AUTH_URL, data=json.dumps(payload), headers=headers) def index(request): - return HttpResponse(PLATFORM_URL, content_type="text/plain") \ No newline at end of file + access_token = get_access_token() + ACCESS_TOKEN = access_token + return HttpResponse(ACCESS_TOKEN, content_type='text/plain') \ No newline at end of file