Browse files

implemented the automatically_authenticate method

  • Loading branch information...
1 parent 10f2cda commit 989dd9e90660ceaa7df3c2d75ba53947eae86e78 @deanmalmgren committed Apr 19, 2012
Showing with 53 additions and 12 deletions.
  1. +53 −12 src/fbconsole.py
View
65 src/fbconsole.py
@@ -347,10 +347,12 @@ def authenticate():
while ACCESS_TOKEN is None:
httpd.handle_request()
-def automatically_authenticate(username=None, password=None,
+def automatically_authenticate(username, password, client_secret,
debug=False):
- """Authenticate with facebook automatically so you can make api
- calls that require authorization.
+ """Authenticate with facebook automatically so that server-side
+ facebook apps can make api calls that require authorization. A
+ username, password, and client_secret must be specified
+ (http://developers.facebook.com/docs/authentication/server-side/)
This method automatically sets the ACCESS_TOKEN so that all
subsequent calls to facebook are authenticated.
@@ -359,9 +361,55 @@ def automatically_authenticate(username=None, password=None,
variable to the list of permissions you want.
"""
+ # use the global APP_ID and AUTH_SCOPE for authentication. this
+ # method sets the ACCESS_TOKEN at the end
+ global APP_ID
+ global AUTH_SCOPE
+ global ACCESS_TOKEN
+
+ # instantiate the browser
browser = _instantiate_browser(debug=debug)
- raise NotImplementedError
+ # the state is a random string that is used in subsequent requests
+ chars = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ state = ''.join((random.choice(chars) for i in range(20)))
+
+ # 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",
+ "scope": ','.join(AUTH_SCOPE),
+ "state": state,
+ })
+ browser.open(url)
+
+ # 2. "user" is prompted to authorize your application
+ browser.select_form(nr=0)
+ browser.form["email"] = username
+ browser.form["pass"] = password
+ response = browser.submit()
+
+ # 3. Once the user is redirected back to our app, parse out the
+ # code generated by facebook
+ auth_url = urlparse(response.geturl())
+ oauth = parse_qs(auth_url.query)
+ assert oauth["state"][0] == state, "%s != %s" % (
+ oauth["state"][0], state,
+ )
+ code = oauth["code"][0]
+
+ # 4. Exchange the code for a user access token for this user's data
+ url="https://graph.facebook.com/oauth/access_token?"+urllib.urlencode({
+ "client_id": APP_ID,
+ "redirect_uri": "http://staging.datascopeanalytics.com/www",
+ "client_secret": client_secret,
+ "code": code,
+ })
+ browser.open(url)
+ response = browser.response()
+ oauth = parse_qs(response.read())
+ ACCESS_TOKEN = oauth["access_token"][0]
def logout():
"""Logout of facebook. This just removes the cached access token."""
@@ -764,11 +812,4 @@ def test_suite():
return doctest.DocTestSuite()
if __name__ == '__main__':
- # shell()
-
- import getpass
- username = raw_input("enter facebook username: ")
- password = getpass.getpass("enter facebook password for '%s': " % username)
-
- automatically_authenticate(username, password, debug=True)
- print "Hello", get('/me')['name']
+ shell()

0 comments on commit 989dd9e

Please sign in to comment.