Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add heroku button #4

Merged
merged 18 commits into from

3 participants

@friism

Supersedes PR #2

app.py
@@ -1,6 +1,8 @@
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
@zheller Collaborator
zheller added a note

please alphabetize imports

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
app.py
@@ -1,6 +1,8 @@
from __future__ import absolute_import
from flask import Flask, render_template, request, redirect, session
from rauth import OAuth2Service
+from urlparse import urlparse
@zheller Collaborator
zheller added a note

standard lib imports should be on their own block above

@friism
friism added a note

I'm guessing the block below (with json and friends)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@zheller zheller commented on the diff
@@ -214,6 +217,11 @@ def me():
data=response.text,
)
+def get_redirect_uri(request):
@zheller Collaborator
zheller added a note

docstring please

@friism
friism added a note

Added

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
app.py
@@ -214,6 +217,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)
@zheller Collaborator
zheller added a note

Our convention is 'my string {variable_name}'.format(variable_name='is awesome')

@friism
friism added a note

Fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@friism

@zheller Thanks for the feedback! Let me know if I missed anything

@zheller
Collaborator

Cool, i think you may need to rebase and fix a merge conflict but its a +1 from me!

@kaushal

Instead of "both OAuth scopes" we should specify that the profile and history scopes are needed specifically.

@friism

@kaushal @zheller changed the scope wording and it's rebased on master

app.py
((8 lines not shown))
import json
+import os
+import requests
@zheller Collaborator
zheller added a note

oops forgot this, requests is not a std lib, you should also swap this section and above -- i think this was a problem in our code initially but would love if you did us a solid here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@friism

@zheller like so? (sorry about this back-and-forth, I don't write Python in my dayjob)

@zheller
Collaborator

looks great! Thanks @friism

@zheller zheller merged commit 0c6989e into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 20, 2014
  1. @friism

    add Procfile

    friism authored
  2. @friism

    add gunicorn to requirements

    friism authored
  3. @friism

    fix Procfile

    friism authored
  4. @friism

    add app.json

    friism authored
  5. @friism

    add README section

    friism authored
  6. @friism

    fix whitespace

    friism authored
  7. @friism

    shorter name

    friism authored
  8. @friism

    generate the redirect url

    friism authored
  9. @friism

    make it cleaner

    friism authored
  10. @friism

    add sslify

    friism authored
  11. @friism

    update README

    friism authored
  12. @friism
Commits on Aug 22, 2014
  1. @friism

    organize imports

    friism authored
  2. @friism

    add docstring for helper method

    friism authored
  3. @friism

    better string format

    friism authored
  4. @friism

    Merge branch 'master' of github.com:uber/Python-Sample-Application in…

    friism authored
    …to add-heroku-button
    
    Conflicts:
    	README.md
  5. @friism

    change scope wording

    friism authored
  6. @friism

    reorganize imports

    friism authored
This page is out of date. Refresh to see the latest.
View
1  Procfile
@@ -0,0 +1 @@
+web: gunicorn app:app --log-file=-
View
10 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 - `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’
@@ -36,6 +36,14 @@ 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)
+
+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
---------------
View
14 app.json
@@ -0,0 +1,14 @@
+{
+ "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"],
+ "env": {
+ "UBER_CLIENT_ID": {
+ "description": "Your Uber API client id"
+ },
+ "UBER_CLIENT_SECRET": {
+ "description": "Your Uber API client secret"
+ }
+ }
+}
View
20 app.py
@@ -1,15 +1,19 @@
from __future__ import absolute_import
+
+import json
+import os
+from urlparse import urlparse
+
from flask import Flask, render_template, request, redirect, session
+from flask_sslify import SSLify
from rauth import OAuth2Service
-
import requests
-import os
-import json
app = Flask(__name__, static_folder='static', static_url_path='')
app.requests_session = requests.Session()
app.secret_key = os.urandom(24)
+sslify = SSLify(app)
with open('config.json') as f:
config = json.load(f)
@@ -49,7 +53,7 @@ def signup():
"""
params = {
'response_type': 'code',
- 'redirect_uri': config.get('redirect_uri'),
+ 'redirect_uri': get_redirect_uri(request),
'scope': config.get('scopes'),
}
url = generate_oauth_service().get_authorize_url(**params)
@@ -64,7 +68,7 @@ def submit():
a code that can be used to obtain an access token for the logged-in use.
"""
params = {
- 'redirect_uri': config.get('redirect_uri'),
+ 'redirect_uri': get_redirect_uri(request),
'code': request.args.get('code'),
'grant_type': 'authorization_code'
}
@@ -214,6 +218,12 @@ def me():
data=response.text,
)
+def get_redirect_uri(request):
@zheller Collaborator
zheller added a note

docstring please

@friism
friism added a note

Added

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ """Returns OAuth redirect URI."""
+ parsed_url = urlparse(request.url)
+ if parsed_url.hostname == 'localhost':
+ 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)
View
1  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/",
View
3  requirements.txt
@@ -9,3 +9,6 @@ nose==1.3.3
rauth==0.7.0
requests==2.3.0
wsgiref==0.1.2
+
+gunicorn==18.0
+Flask-SSLify==0.1.4
Something went wrong with that request. Please try again.