Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get Access Token #1

Merged
merged 2 commits into from Jun 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Pipfile
Expand Up @@ -15,6 +15,7 @@ python_version = "3.6"
"psycopg2-binary" = "*"
django-heroku = "*"
gunicorn = "*"
requests = "*"


[dev-packages]
Expand Down
4 changes: 3 additions & 1 deletion README.md
Expand Up @@ -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`
Expand Down
25 changes: 24 additions & 1 deletion 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")
access_token = get_access_token()
ACCESS_TOKEN = access_token
return HttpResponse(ACCESS_TOKEN, content_type='text/plain')