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

Added microsoft example #69

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions example/microsoft.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
from flask import Flask, redirect, url_for, session
from flask_oauth import OAuth


MICROSOFT_CLIENT_ID = 'CLIENT_ID'
MICROSOFT_CLIENT_SECRET = 'CLIENT_SECRET'
REDIRECT_URI = '/microsoft_callback'

SECRET_KEY = 'development key'
DEBUG = True

app = Flask(__name__)
app.debug = DEBUG
app.secret_key = SECRET_KEY
oauth = OAuth()

microsoft = oauth.remote_app('microsoft',
base_url='https://login.live.com',
authorize_url='https://login.live.com/oauth20_authorize.srf',
request_token_url=None,
request_token_params={'scope': 'wl.signin wl.emails',
'response_type': 'code'},
access_token_url='https://login.live.com/oauth20_token.srf',
access_token_method='POST',
access_token_params={'grant_type': 'authorization_code'},
consumer_key=MICROSOFT_CLIENT_ID,
consumer_secret=MICROSOFT_CLIENT_SECRET)

@app.route('/')
def index():
access_token = session.get('access_token')
if access_token is None:
return redirect(url_for('login'))

access_token = access_token[0]
from urllib2 import Request, urlopen, URLError

# headers = {'Authorization': 'OAuth '+access_token}
req = Request('https://apis.live.net/v5.0/me?access_token=%s' % access_token)
try:
res = urlopen(req)
except URLError, e:
if e.code == 401:
# Unauthorized - bad token
session.pop('access_token', None)
return redirect(url_for('login'))
return res.read()

return res.read()


@app.route('/login')
def login():
callback=url_for('authorized', _external=True)
return microsoft.authorize(callback=callback)



@app.route(REDIRECT_URI)
@microsoft.authorized_handler
def authorized(resp):
access_token = resp['access_token']
print "access_token:", access_token
session['access_token'] = access_token, ''
return redirect(url_for('index'))


@microsoft.tokengetter
def get_access_token():
return session.get('access_token')


def main():
app.run()


if __name__ == '__main__':
main()