Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

make Google OAuth2 works #25

Merged
merged 3 commits into from

3 participants

@mete0r
  • include Matt Wright's access_token_params patch
  • handle_oauth2_response() makes access token requests properly when the method is POST
  • a working Google OAuth2 example
Matt Wright and others added some commits
Matt Wright Add access_token_params to allow for optional parameters for Oauth2 a…
…ccess_token calls. This was specifically needed for foursquare's API. They require a parameter named 'grant_type'
fb2e27b
@mete0r mete0r Google OAuth2 example e3df26b
@mete0r mete0r Proper access token request params in regard to the request methods
If access_token_method is 'POST', encode parameters in the request body
eb35fb0
@mitsuhiko mitsuhiko merged commit 80167c4 into mitsuhiko:master
@mitsuhiko
Owner

Looks good.

@mattupstate

This is a nice update an it would be greatly appreciated if you could push an update to pypi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 12, 2012
  1. @mete0r

    Add access_token_params to allow for optional parameters for Oauth2 a…

    Matt Wright authored mete0r committed
    …ccess_token calls. This was specifically needed for foursquare's API. They require a parameter named 'grant_type'
  2. @mete0r

    Google OAuth2 example

    mete0r authored
  3. @mete0r

    Proper access token request params in regard to the request methods

    mete0r authored
    If access_token_method is 'POST', encode parameters in the request body
This page is out of date. Refresh to see the latest.
Showing with 91 additions and 2 deletions.
  1. +76 −0 example/google.py
  2. +15 −2 flaskext/oauth.py
View
76 example/google.py
@@ -0,0 +1,76 @@
+from flask import Flask, redirect, url_for, session
+from flaskext.oauth import OAuth
+
+
+# You must configure these 3 values from Google APIs console
+# https://code.google.com/apis/console
+GOOGLE_CLIENT_ID = '<Client-ID>'
+GOOGLE_CLIENT_SECRET = '<Client-secret>'
+REDIRECT_URI = '/authorized' # one of the Redirect URIs from Google APIs console
+
+SECRET_KEY = 'development key'
+DEBUG = True
+
+app = Flask(__name__)
+app.debug = DEBUG
+app.secret_key = SECRET_KEY
+oauth = OAuth()
+
+google = oauth.remote_app('google',
+ base_url='https://www.google.com/accounts/',
+ authorize_url='https://accounts.google.com/o/oauth2/auth',
+ request_token_url=None,
+ request_token_params={'scope': 'https://www.googleapis.com/auth/userinfo.email',
+ 'response_type': 'code'},
+ access_token_url='https://accounts.google.com/o/oauth2/token',
+ access_token_method='POST',
+ access_token_params={'grant_type': 'authorization_code'},
+ consumer_key=GOOGLE_CLIENT_ID,
+ consumer_secret=GOOGLE_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://www.googleapis.com/oauth2/v1/userinfo',
+ None, headers)
+ try:
+ res = urlopen(req)
+ except URLError:
+ return res.read()
+
+ return res.read()
+
+
+@app.route('/login')
+def login():
+ callback=url_for('authorized', _external=True)
+ return google.authorize(callback=callback)
+
+
+
+@app.route(REDIRECT_URI)
+@google.authorized_handler
+def authorized(resp):
+ access_token = resp['access_token']
+ session['access_token'] = access_token, ''
+ return redirect(url_for('index'))
+
+
+@google.tokengetter
+def get_access_token():
+ return session.get('access_token')
+
+
+def main():
+ app.run()
+
+
+if __name__ == '__main__':
+ main()
View
17 flaskext/oauth.py
@@ -162,6 +162,8 @@ class OAuthRemoteApp(object):
to forward to the request token URL
or authorize URL depending on oauth
version.
+ :param access_token_params: an option diction of parameters to forward to
+ the access token URL
:param access_token_method: the HTTP method that should be used
for the access_token_url. Defaults
to ``'GET'``.
@@ -172,6 +174,7 @@ def __init__(self, oauth, name, base_url,
access_token_url, authorize_url,
consumer_key, consumer_secret,
request_token_params=None,
+ access_token_params=None,
access_token_method='GET'):
self.oauth = oauth
#: the `base_url` all URLs are joined with.
@@ -184,6 +187,7 @@ def __init__(self, oauth, name, base_url,
self.consumer_secret = consumer_secret
self.tokengetter_func = None
self.request_token_params = request_token_params or {}
+ self.access_token_params = access_token_params or {}
self.access_token_method = access_token_method
self._consumer = oauth2.Consumer(self.consumer_key,
self.consumer_secret)
@@ -350,8 +354,17 @@ def handle_oauth2_response(self):
'client_secret': self.consumer_secret,
'redirect_uri': session.get(self.name + '_oauthredir')
}
- url = add_query(self.expand_url(self.access_token_url), remote_args)
- resp, content = self._client.request(url, self.access_token_method)
+ remote_args.update(self.access_token_params)
+ if self.access_token_method == 'POST':
+ resp, content = self._client.request(self.access_token_url,
+ self.access_token_method,
+ url_encode(remote_args))
+ elif self.access_token_method == 'GET':
+ url = add_query(self.expand_url(self.access_token_url), remote_args)
+ resp, content = self._client.request(url, self.access_token_method)
+ else:
+ raise OAuthException('Unsupported access_token_method: ' +
+ self.access_token_method)
data = parse_response(resp, content)
if resp['status'] != '200':
raise OAuthException('Invalid response from ' + self.name, data)
Something went wrong with that request. Please try again.