Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Handle 4sq connect errors cleanly

  • Loading branch information...
commit a287f1514667a865ae4afca67491d6214bcf4798 1 parent c66b367
@smehmood smehmood authored
Showing with 46 additions and 8 deletions.
  1. +2 −1  config.py
  2. +31 −7 main.py
  3. +13 −0 templates/connect_error.html
View
3  config.py
@@ -25,9 +25,10 @@
'local_dev': True,
# AppEngine debug mode
'debug': True,
- # Thesse can either be a path (on this server), or an external URI
+ # These can either be a path (on this server), or an external URI
'auth_success_uri_desktop': '/',
'auth_success_uri_mobile': '/',
+ 'auth_denied_uri': '/',
# Application Level Configuration
# Feel free to add new config parameters here...
}
View
38 main.py
@@ -20,21 +20,45 @@
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template
+class OAuthConnectErrorException(Exception):
+ pass
+
+class OAuthConnectDeniedException(Exception):
+ pass
+
+
class OAuth(webapp.RequestHandler):
"""Handle the OAuth redirect back to the service."""
def post(self):
self.get()
def get(self):
- code = self.request.get('code')
- assert code, "No code, bailing"
+ try:
+ error = self.request.get('error')
+ if error == "access_denied":
+ raise OAuthConnectDeniedException
+ elif error:
+ raise OAuthConnectErrorException
+
+ code = self.request.get('code')
+ if not code:
+ raise ConnectException
+
+ client = utils.makeFoursquareClient()
+ access_token = client.oauth.get_token(code)
+ if not access_token:
+ raise ConnectException
+ client.set_access_token(access_token)
+ except OAuthConnectDeniedException:
+ self.redirect(CONFIG['auth_denied_uri'])
+ return
+ except OAuthConnectErrorException:
+ path = os.path.join(os.path.dirname(__file__),
+ 'templates/connect_error.html')
+ self.response.out.write(template.render(path, {'name': CONFIG['site_name']}))
+ return
- client = utils.makeFoursquareClient()
- access_token = client.oauth.get_token(code)
- assert access_token, "Failed to get access_token from foursquare."
- client.set_access_token(access_token)
user = client.users() # returns the auth'd users info
-
fs_user_id = user['user']['id']
existing_token = UserToken.get_by_fs_id(fs_user_id)
View
13 templates/connect_error.html
@@ -0,0 +1,13 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
+<html>
+<head>
+ <title>{{ name|escape }}</title>
+ <meta content="width=320" name="viewport" />
+</head>
+<body>
+<h1>Something broke!</h1>
+ <div class="description">
+ <p>There was a problem connecting your foursquare account to {{ name|escape }}. Please try again.</p>
+ </div>
+</body>
+</html>
Please sign in to comment.
Something went wrong with that request. Please try again.