Created
January 9, 2012 23:20
-
-
Save deadprogram/1585581 to your computer and use it in GitHub Desktop.
Python Sample Application for AT&T Alpha API Foundry
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AUTHORIZATION_URL = 'https://auth.tfoundry.com/oauth/' | |
CALLBACK_URL = 'http://hello-foundry-py.herokuapp.com/auth/callback' | |
RESOURCE_URL = 'https://hello-att-foundry.heroku.com/hello.json' | |
CONSUMER_KEY = 'key' | |
CONSUMER_SECRET = 'secret' | |
import os | |
import urlparse | |
import oauth2 | |
import json | |
client = oauth2.Client2(CONSUMER_KEY, CONSUMER_SECRET, AUTHORIZATION_URL) | |
from flask import Flask, redirect, request | |
app = Flask(__name__) | |
@app.route('/') | |
def index(): | |
return '<p><a href="/auth">Try to authorize</a>.</p>' | |
@app.route('/auth') | |
def auth(): | |
auth_url = client.authorization_url(redirect_uri = CALLBACK_URL, params={'response_type' : "client_credentials"}) | |
print auth_url | |
return redirect(auth_url) | |
@app.route('/auth/callback') | |
def auth_callback(): | |
code = request.args.get('code', '') | |
token = client.access_token(code, CALLBACK_URL, endpoint='token')["access_token"] | |
(headers, content) = client.request(RESOURCE_URL, access_token=token) | |
if headers['status'] != '200': | |
raise Exception(content) | |
greeting = json.loads(content)["greeting"] | |
return "Greeting: " + greeting | |
if __name__ == '__main__': | |
port = int(os.environ.get("PORT", 5000)) | |
app.run(host='0.0.0.0', port=port) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
web: python hi.py |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Flask | |
git+git://github.com/hybridgroup/python-oauth2.git#egg=oauth2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment