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

Flask Oauth with major provider (Facebook,Google,Twitter) #9

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
34 changes: 34 additions & 0 deletions flask-facebok-google-twitter-login/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Flask Oauth with major providers

You can learn Flask OAuth client with this demo.

## Install

Install the required dependencies:

$ pip install -U Flask Authlib requests

## Config

Google:

Create your Google OAuth Client at <https://console.cloud.google.com/apis/credentials>, make sure to add `http://localhost:5000/google/auth/` into Authorized redirect URIs.

Twitter:
Create your Twitter Oauth 1.0 Client at <https://developer.twitter.com/> by creating a app.
Add `http://localhost:5000/twitter/auth/` into Authorized redirect URIs.

Facebook:
Create your Facebook OAuth Client at <https://developer.facebook.com/>, by creating a app.
Add `http://localhost:5000/facebook/auth/` into Authorized redirect URIs.

Make sure you enter the key and secret for every provider in the app.py or use them as a environment variable
## Run

Start server with:

python app.py

Then visit:

http://localhost:5000/
24 changes: 18 additions & 6 deletions flask-multiple-login/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
from flask import render_template, redirect, abort
from authlib.integrations.flask_client import OAuth


app = Flask(__name__)
app.secret_key = '!secret'
app.secret_key = '\xfd{H\xe5<\x95\xf9\xe3\x96.5\xd1\x01O<!/\xd5\xa2\xa0\x9fR"\xa1\xa8'
app.config.from_object('config')

CONF_URL = 'https://accounts.google.com/.well-known/openid-configuration'
Expand Down Expand Up @@ -45,6 +44,16 @@ def normalize_twitter_userinfo(client, data):
fetch_token=lambda: session.get('token'), # DON'T DO IT IN PRODUCTION
)

oauth.register(
name='facebook',
access_token_url = 'https://graph.facebook.com/oauth/access_token',
access_token_params = None,
authorize_url = 'https://www.facebook.com/dialog/oauth',
authorize_params = None,
api_base_url = 'https://graph.facebook.com/',
client_kwargs={'scope': 'email'},
)



@app.route('/')
Expand All @@ -68,12 +77,15 @@ def auth(name):
client = oauth.create_client(name)
if not client:
abort(404)

token = client.authorize_access_token()
user = token.get('userinfo')
if not user:
if 'id_token' in token:
user = client.parse_id_token(token)
elif name == "facebook":
resp = oauth.facebook.get(
'https://graph.facebook.com/me?fields=id,name,email,picture{url}')
user = resp.json()
else:
user = client.userinfo()

session['user'] = user
return redirect('/')

Expand Down
3 changes: 3 additions & 0 deletions flask-multiple-login/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@

TWITTER_CLIENT_ID = os.getenv('TWITTER_CLIENT_ID')
TWITTER_CLIENT_SECRET = os.getenv('TWITTER_CLIENT_SECRET')

FACEBOOK_CLIENT_ID = os.environ.get('FACEBOOK_CLIENT_ID')
FACEBOOK_CLIENT_SECRET = os.environ.get('FACEBOOK_CLIENT_SECRET')
1 change: 1 addition & 0 deletions flask-multiple-login/templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
{% else %}
<a href="/login/twitter">twitter login</a>
<a href="/login/google">google login</a>
<a href="/login/facebook">facebook login</a>
{% endif %}