From 30498641f06b45561bc1e8a5ac1abe6abf91225b Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 20 Aug 2014 09:19:28 -0700 Subject: [PATCH 01/17] add Procfile --- Procfile | 1 + 1 file changed, 1 insertion(+) create mode 100644 Procfile diff --git a/Procfile b/Procfile new file mode 100644 index 0000000..c94769d --- /dev/null +++ b/Procfile @@ -0,0 +1 @@ +web: gunicorn hello:app --log-file=- From 00caefe2dbe9b88c552a280d95247f9ed023ff13 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 20 Aug 2014 09:31:06 -0700 Subject: [PATCH 02/17] add gunicorn to requirements --- requirements.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/requirements.txt b/requirements.txt index 6c53228..cfecbb8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -9,3 +9,5 @@ nose==1.3.3 rauth==0.7.0 requests==2.3.0 wsgiref==0.1.2 + +gunicorn==18.0 From 8ea075b69389697891a467c9137aa9910b367db9 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 20 Aug 2014 09:40:57 -0700 Subject: [PATCH 03/17] fix Procfile --- Procfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Procfile b/Procfile index c94769d..244c130 100644 --- a/Procfile +++ b/Procfile @@ -1 +1 @@ -web: gunicorn hello:app --log-file=- +web: gunicorn app:app --log-file=- From f1e4cc872acc7be8c180288e6152894c9d721390 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 20 Aug 2014 10:02:39 -0700 Subject: [PATCH 04/17] add app.json --- app.json | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 app.json diff --git a/app.json b/app.json new file mode 100644 index 0000000..855957d --- /dev/null +++ b/app.json @@ -0,0 +1,14 @@ +{ + "name": "Flask sample app using the Uber API", + "logo": "http://blogcdn.uber.com/wp-content/uploads/2011/12/New-Logo-Vertical-Dark.jpg", + "repository": "https://github.com/uber/Python-Sample-Application", + "keywords": ["uber", "python", "flask"], + "env": { + "UBER_CLIENT_ID": { + "description": "Your Uber API client id" + }, + "UBER_CLIENT_SECRET": { + "description": "Your Uber API client secret" + } + } +} From e5e4f8f918bd39fc0a0cc5c24dccf9f026c23afa Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 20 Aug 2014 10:04:45 -0700 Subject: [PATCH 05/17] add README section --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index ba41873..9d85b1b 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,12 @@ If you want to work on this application we’d love your pull requests and ticke 1. If you open up a ticket, please make sure it describes the problem or feature request fully. 2. If you send us a pull request, make sure you add a test for what you added, and make sure the full test suite runs with nosetests -v. +Deploy to Heroku +---------------- + +Click the buttom below to set up this sample app on Heroku: + +[![Deploy](https://www.herokucdn.com/deploy/button.png)](https://heroku.com/deploy) Making Requests --------------- From 36603908d1a7f63ce18a340a21e96becd9f7e8f4 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 20 Aug 2014 10:06:44 -0700 Subject: [PATCH 06/17] fix whitespace --- app.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app.json b/app.json index 855957d..ed3d33d 100644 --- a/app.json +++ b/app.json @@ -5,7 +5,7 @@ "keywords": ["uber", "python", "flask"], "env": { "UBER_CLIENT_ID": { - "description": "Your Uber API client id" + "description": "Your Uber API client id" }, "UBER_CLIENT_SECRET": { "description": "Your Uber API client secret" From d0585c88ffd93f7418db8b4b69b5212a1d3072d7 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 20 Aug 2014 10:08:42 -0700 Subject: [PATCH 07/17] shorter name --- app.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app.json b/app.json index ed3d33d..a0bee26 100644 --- a/app.json +++ b/app.json @@ -1,5 +1,5 @@ { - "name": "Flask sample app using the Uber API", + "name": "Uber API Python/Flask sample", "logo": "http://blogcdn.uber.com/wp-content/uploads/2011/12/New-Logo-Vertical-Dark.jpg", "repository": "https://github.com/uber/Python-Sample-Application", "keywords": ["uber", "python", "flask"], From c93495a425ab68c6f03bcdee3819de0487af50f7 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 20 Aug 2014 10:49:16 -0700 Subject: [PATCH 08/17] generate the redirect url --- app.py | 7 +++++-- config.json | 1 - 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/app.py b/app.py index c9e2eb6..45dcc89 100644 --- a/app.py +++ b/app.py @@ -1,6 +1,7 @@ from __future__ import absolute_import from flask import Flask, render_template, request, redirect, session from rauth import OAuth2Service +from urlparse import urlparse import requests import os @@ -47,9 +48,10 @@ def signup(): You should navigate here first. It will redirect to login.uber.com. """ + parsed_url = urlparse(request.url) params = { 'response_type': 'code', - 'redirect_uri': config.get('redirect_uri'), + 'redirect_uri': parsed_url.scheme + parsed_url.hostname + '/submit', 'scope': config.get('scopes'), } url = generate_oauth_service().get_authorize_url(**params) @@ -63,8 +65,9 @@ def submit(): Your redirect uri will redirect you here, where you will exchange a code that can be used to obtain an access token for the logged-in use. """ + parsed_url = urlparse(request.url) params = { - 'redirect_uri': config.get('redirect_uri'), + 'redirect_uri': parsed_url.scheme + parsed_url.hostname + '/submit', 'code': request.args.get('code'), 'grant_type': 'authorization_code' } diff --git a/config.json b/config.json index bea68d4..1c390a9 100644 --- a/config.json +++ b/config.json @@ -1,5 +1,4 @@ { - "redirect_uri": "http://localhost:7000/submit", "access_token_url": "https://login.uber.com/oauth/token", "authorize_url": "https://login.uber.com/oauth/authorize", "base_url": "https://login.uber.com/", From 6d467cfa7fe4fb8a6bef08a8fd23f01d3affe01f Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 20 Aug 2014 11:22:28 -0700 Subject: [PATCH 09/17] make it cleaner --- app.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/app.py b/app.py index 45dcc89..0fa3adc 100644 --- a/app.py +++ b/app.py @@ -48,10 +48,9 @@ def signup(): You should navigate here first. It will redirect to login.uber.com. """ - parsed_url = urlparse(request.url) params = { 'response_type': 'code', - 'redirect_uri': parsed_url.scheme + parsed_url.hostname + '/submit', + 'redirect_uri': get_redirect_uri(request), 'scope': config.get('scopes'), } url = generate_oauth_service().get_authorize_url(**params) @@ -65,9 +64,8 @@ def submit(): Your redirect uri will redirect you here, where you will exchange a code that can be used to obtain an access token for the logged-in use. """ - parsed_url = urlparse(request.url) params = { - 'redirect_uri': parsed_url.scheme + parsed_url.hostname + '/submit', + 'redirect_uri': get_redirect_uri(request), 'code': request.args.get('code'), 'grant_type': 'authorization_code' } @@ -217,6 +215,11 @@ def me(): data=response.text, ) +def get_redirect_uri(request): + parsed_url = urlparse(request.url) + if parsed_url.hostname == 'localhost': + return str.format('http://{0}:{1}/submit', parsed_url.hostname, parsed_url.port) + return str.format('https://{0}/submit', parsed_url.hostname) if __name__ == '__main__': app.run(port=7000) From cb18b042db5ca228d66432da60973c5b26255465 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 20 Aug 2014 11:30:41 -0700 Subject: [PATCH 10/17] add sslify --- app.py | 2 ++ requirements.txt | 1 + 2 files changed, 3 insertions(+) diff --git a/app.py b/app.py index 0fa3adc..5af09c9 100644 --- a/app.py +++ b/app.py @@ -2,6 +2,7 @@ from flask import Flask, render_template, request, redirect, session from rauth import OAuth2Service from urlparse import urlparse +from flask_sslify import SSLify import requests import os @@ -11,6 +12,7 @@ app.requests_session = requests.Session() app.secret_key = os.urandom(24) +sslify = SSLify(app) with open('config.json') as f: config = json.load(f) diff --git a/requirements.txt b/requirements.txt index cfecbb8..4f8fb11 100644 --- a/requirements.txt +++ b/requirements.txt @@ -11,3 +11,4 @@ requests==2.3.0 wsgiref==0.1.2 gunicorn==18.0 +Flask-SSLify==0.1.4 \ No newline at end of file From 8060cce63810e7293180807ece8e73899c095a83 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 20 Aug 2014 11:42:01 -0700 Subject: [PATCH 11/17] update README --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 9d85b1b..6a4c42c 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,8 @@ Click the buttom below to set up this sample app on Heroku: [![Deploy](https://www.herokucdn.com/deploy/button.png)](https://heroku.com/deploy) +After creating your app on Heroku, you have to configure the redirect url for your Uber OAuth app. Use a `https://{my-app-name}.herokuapp.com/submit` url. + Making Requests --------------- From 29e7b0b9af3ae542b563bce71984d9456b930550 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 20 Aug 2014 11:42:43 -0700 Subject: [PATCH 12/17] note that both OAuth scopes are required --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6a4c42c..9c99d3d 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ How To Use This --------------- 1. Navigate over to https://developer.uber.com/, and sign up for an Uber developer account. -2. Register a new Uber application and make your Redirect URI http://localhost:7000/submit +2. Register a new Uber application and make your Redirect URI http://localhost:7000/submit - both OAuth scopes are required 3. Fill in the relevant information in the config.json file in the root folder and add your client id and secret as the environment variables UBER_CLIENT_ID AND UBER_CLIENT_SECRET. Run ‘export UBER_CLIENT_ID=”YOUR_CLIENT_ID”&&export UBER_CLIENT_SECRET=”YOUR_CLIENT_SECRET”’ 4. Run ‘pip install -r requirements.txt’ to install dependencies 5. Run ‘python app.py’ From d04e17c00227b8cc43736742dc57080c195fdb42 Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 21 Aug 2014 17:08:29 -0700 Subject: [PATCH 13/17] organize imports --- app.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app.py b/app.py index 5af09c9..2b8d96e 100644 --- a/app.py +++ b/app.py @@ -1,12 +1,12 @@ from __future__ import absolute_import from flask import Flask, render_template, request, redirect, session -from rauth import OAuth2Service -from urlparse import urlparse from flask_sslify import SSLify +from rauth import OAuth2Service -import requests -import os import json +import os +import requests +from urlparse import urlparse app = Flask(__name__, static_folder='static', static_url_path='') app.requests_session = requests.Session() From 1275e276cb38053b63215f1566398448c8000bdc Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 21 Aug 2014 17:14:14 -0700 Subject: [PATCH 14/17] add docstring for helper method --- app.py | 1 + 1 file changed, 1 insertion(+) diff --git a/app.py b/app.py index 2b8d96e..b268fb4 100644 --- a/app.py +++ b/app.py @@ -218,6 +218,7 @@ def me(): ) def get_redirect_uri(request): + """Returns OAuth redirect URI.""" parsed_url = urlparse(request.url) if parsed_url.hostname == 'localhost': return str.format('http://{0}:{1}/submit', parsed_url.hostname, parsed_url.port) From 8188458141df5a2a736f74e1f75e06d60caa46c8 Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 21 Aug 2014 17:16:12 -0700 Subject: [PATCH 15/17] better string format --- app.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app.py b/app.py index b268fb4..3e75737 100644 --- a/app.py +++ b/app.py @@ -221,8 +221,8 @@ def get_redirect_uri(request): """Returns OAuth redirect URI.""" parsed_url = urlparse(request.url) if parsed_url.hostname == 'localhost': - return str.format('http://{0}:{1}/submit', parsed_url.hostname, parsed_url.port) - return str.format('https://{0}/submit', parsed_url.hostname) + return 'http://{hostname}:{port}/submit'.format(hostname=parsed_url.hostname, port=parsed_url.port) + return 'https://{hostname}/submit'.format(hostname=parsed_url.hostname) if __name__ == '__main__': app.run(port=7000) From e62fa12994acba30a27d45eba04fb1306fdbc83d Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 21 Aug 2014 17:34:15 -0700 Subject: [PATCH 16/17] change scope wording --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 70eba9c..64e403c 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ How To Use This --------------- 1. Navigate over to https://developer.uber.com/, and sign up for an Uber developer account. -2. Register a new Uber application and make your Redirect URI http://localhost:7000/submit - both OAuth scopes are required +2. Register a new Uber application and make your Redirect URI http://localhost:7000/submit - `profile` and `history` OAuth scopes are required 3. Fill in the relevant information in the config.json file in the root folder and add your client id and secret as the environment variables UBER_CLIENT_ID AND UBER_CLIENT_SECRET. Run 'export UBER_CLIENT_ID="YOUR_CLIENT_ID"&&export UBER_CLIENT_SECRET="YOUR_CLIENT_SECRET"' 4. Run ‘pip install -r requirements.txt’ to install dependencies 5. Run ‘python app.py’ From 6a5c1e0cb89baf20486fea09e5af5d60d697bd01 Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 21 Aug 2014 17:51:51 -0700 Subject: [PATCH 17/17] reorganize imports --- app.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/app.py b/app.py index 94d9e67..4a56eb4 100644 --- a/app.py +++ b/app.py @@ -1,13 +1,14 @@ from __future__ import absolute_import -from flask import Flask, render_template, request, redirect, session -from flask_sslify import SSLify -from rauth import OAuth2Service import json import os -import requests from urlparse import urlparse +from flask import Flask, render_template, request, redirect, session +from flask_sslify import SSLify +from rauth import OAuth2Service +import requests + app = Flask(__name__, static_folder='static', static_url_path='') app.requests_session = requests.Session() app.secret_key = os.urandom(24)