Permalink
Browse files

added AutomaticLoginError and exception handling to capture incorrect…

… credentials on login
  • Loading branch information...
deanmalmgren committed Apr 19, 2012
1 parent 59a7c09 commit 6777b40c3b160bb071b81cd661dabefb6a62e1bf
Showing with 15 additions and 3 deletions.
  1. +15 −3 src/fbconsole.py
View
@@ -235,6 +235,15 @@ class UnknownApiException(ApiException):
class OAuthException(ApiException):
"""Just an oath exception."""
+class AutomaticLoginError(Exception):
+ """
+ An error has occurred during login. This can occur for a number
+ of reasons. Make sure you have correctly specified the username,
+ password, client_secret, redirect_uri, and APP_ID for your
+ facebook app.
+ """
+ def __str__(self):
+ return self.__class__.__doc__
def _handle_http_error(e):
body = e.read()
@@ -347,7 +356,7 @@ def authenticate():
while ACCESS_TOKEN is None:
httpd.handle_request()
-def automatically_authenticate(username, password, client_secret,
+def automatically_authenticate(username, password, client_secret, redirect_uri,
debug=False):
"""Authenticate with facebook automatically so that server-side
facebook apps can make api calls that require authorization. A
@@ -372,13 +381,13 @@ def automatically_authenticate(username, password, client_secret,
# the state is a random string that is used in subsequent requests
chars = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"
- state = ''.join((random.choice(chars) for i in range(20)))
+ state = ''.join((random.choice(chars) for i in range(30)))
# 1. redirect the "user" (a server-side user, in this case) to the
# OAuth dialog
url = "https://www.facebook.com/dialog/oauth?" + urllib.urlencode({
"client_id": APP_ID,
- "redirect_uri": "http://staging.datascopeanalytics.com/www",
+ "redirect_uri": redirect_uri,
"scope": ','.join(AUTH_SCOPE),
"state": state,
})
@@ -394,6 +403,9 @@ def automatically_authenticate(username, password, client_secret,
# code generated by facebook
auth_url = urlparse(response.geturl())
oauth = parse_qs(auth_url.query)
+ if "state" not in oauth:
+ raise AutomaticLoginError
+
assert oauth["state"][0] == state, "%s != %s" % (
oauth["state"][0], state,
)

0 comments on commit 6777b40

Please sign in to comment.