Skip to content

Commit

Permalink
bitly-flask-oauth
Browse files Browse the repository at this point in the history
  • Loading branch information
kingink committed Jul 3, 2013
1 parent 43622a6 commit 3d5d649
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Procfile
Original file line number Diff line number Diff line change
@@ -1 +1 @@
web: gunicorn hello:app
web: gunicorn bitly_oauth:app
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Bitly Oauth Python (Flask) Example


## Setup on bitly

Go to http://bitly.com/a/oauth_apps and follow the steps to register a new application.

Make sure to use your Client ID and Client Secret in index.html.erb and welcome_controller.rb.


## The files
This example is modeled after [bitly-oauth-rails](https://github.com/kingink/bitly-oauth-rails)

[templates/welcome.html](https://github.com/kingink/bitly-oauth-flask/blob/sanitize/templates/welcome.html) - Simple link that starts the authentication process. This will take the user to a bitly login page.

[bitly_oauth.py](https://github.com/kingink/bitly-oauth-flask/blob/sanitize/bitly_oauth.py) - This is were the meat is. After the user logs in they will be redirected back here for more of the oauth process and to get the user access token.

[templates/oauth.html](https://github.com/kingink/bitly-oauth-flask/blob/sanitize/templates/oauth.html) - Access_token and Login will show on this page.


##Links

[bitly Developer Website](http://dev.bitly.com/)

[More on bitly Authentication](http://dev.bitly.com/authentication.html)
32 changes: 32 additions & 0 deletions bitly_oauth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import os
import requests
from flask import Flask
from flask import render_template
from flask import request

app = Flask(__name__)

@app.route('/')
def welcome():
return render_template('welcome.html')

@app.route('/oauth/')
def oauth():
code = request.args.get('code')
client_id = 'YOUR_CLIENT_ID'
client_secret = 'YOUR_CLIENT_SECRET'
redirect_uri = 'YOUR_REDIRECT_URI'

payload = {'client_id': client_id, 'client_secret': client_secret, 'redirect_uri': redirect_uri, 'code': code}
r = requests.post("https://api-ssl.bitly.com/oauth/access_token", data=payload)

data = {}
pairs = r.text.split('&')
for pair in pairs:
k, v = pair.split('=')
data[k] = v

return render_template('oauth.html', login=data['login'], access_token=data['access_token'])

if __name__ == '__main__':
app.run()
8 changes: 0 additions & 8 deletions hello.py

This file was deleted.

1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ Werkzeug==0.9.1
distribute==0.6.34
gunicorn==0.17.4
itsdangerous==0.21
requests==1.2.3
wsgiref==0.1.2
3 changes: 3 additions & 0 deletions templates/oauth.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
login: {{ login }}
<br />
access_token: {{ access_token }}
1 change: 1 addition & 0 deletions templates/welcome.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<a href="https://bitly.com/oauth/authorize?client_id=YOUR_CLIENT_ID&redirect_uri=YOUR_REDIRECT_URI">click here to authorize using your bitly account</a>

0 comments on commit 3d5d649

Please sign in to comment.