[
  {
    "path": ".gitignore",
    "content": "*.py?\n*.egg-info\n"
  },
  {
    "path": ".project",
    "content": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<projectDescription>\r\n\t<name>pyfacebook</name>\r\n\t<comment></comment>\r\n\t<projects>\r\n\t</projects>\r\n\t<buildSpec>\r\n\t\t<buildCommand>\r\n\t\t\t<name>org.python.pydev.PyDevBuilder</name>\r\n\t\t\t<arguments>\r\n\t\t\t</arguments>\r\n\t\t</buildCommand>\r\n\t</buildSpec>\r\n\t<natures>\r\n\t\t<nature>org.python.pydev.pythonNature</nature>\r\n\t</natures>\r\n</projectDescription>\r\n"
  },
  {
    "path": ".pydevproject",
    "content": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\r\n<?eclipse-pydev version=\"1.0\"?>\r\n\r\n<pydev_project>\r\n<pydev_property name=\"org.python.pydev.PYTHON_PROJECT_VERSION\">python 2.6</pydev_property>\r\n<pydev_property name=\"org.python.pydev.PYTHON_PROJECT_INTERPRETER\">Default</pydev_property>\r\n</pydev_project>\r\n"
  },
  {
    "path": "MANIFEST.in",
    "content": "recursive-include examples *\n"
  },
  {
    "path": "README",
    "content": "== PyFacebook ==\n\nPyFacebook is a Python client library for the Facebook API.\n\nSamuel Cormier-Iijima (sciyoshi@gmail.com)\nNiran Babalola (iamniran@gmail.com)\nShannon -jj Behrens (jjinux@gmail.com)\nDavid B. Edelstein (David.B.Edelstein@gmail.com)\nMax Battcher (max.battcher@gmail.com)\nRohan Deshpande (rohan.deshpande@gmail.com)\nMatthew Stevens (matthew.stevens@gmail.com)\nSandro Turriate (sandro.turriate@gmail.com)\nBenjamin Zimmerman (benjamin.zimmerman@gmail.com)\nGisle Aas (gisle.aas@gmail.com)\nRand Bradley (rand.bradley@gmail.com)\nLuke Worth (luke.worth@gmail.com)\nAndreas Cederström (andreas@klydd.se)\nSamuel Hoffstaetter (samuel@hoffstaetter.com)\nAndreas Ehn (ehn@a8n.se)\nLee Li (shuge.lee@gmail.com)\n\nhttp://github.com/sciyoshi/pyfacebook/\n\n== Usage ==\n\nTo use this in your own projects, do the standard:\n\npython setup.py install\n\n\n== Documentation ==\n\nHave a look at the examples/ directory. Most of the stuff should be\nself-explanatory. There is also an example Django app in\nexamples/facebook, which should have enough to get you started.\n\nSee the developer wiki for more information.\n\n\n== License ==\n\nCopyright (c) 2008, Samuel Cormier-Iijima\nAll rights reserved.\n\nRedistribution and use in source and binary forms, with or without\nmodification, are permitted provided that the following conditions are met:\n    * Redistributions of source code must retain the above copyright\n      notice, this list of conditions and the following disclaimer.\n    * Redistributions in binary form must reproduce the above copyright\n      notice, this list of conditions and the following disclaimer in the\n      documentation and/or other materials provided with the distribution.\n    * Neither the name of the author nor the names of its contributors may\n      be used to endorse or promote products derived from this software\n      without specific prior written permission.\n\nTHIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS``AS IS'' AND ANY\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\nDISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n\n"
  },
  {
    "path": "bin/djangofb.py",
    "content": "#!/usr/bin/env python\n\nif __name__ == '__main__':\n    import sys\n    import os\n    import re\n\n    def usage():\n        sys.stderr.write('Usage: djangofb.py startapp <appname>\\n')\n        sys.exit(1)\n\n    if len(sys.argv) not in (2, 3):\n        usage()\n\n    if sys.argv[1] != 'startapp':\n        usage()\n\n    app_name = len(sys.argv) == 3 and sys.argv[2] or 'fbapp'\n\n    try:\n        sys.path.insert(0, os.getcwd())\n        # Assumed to be in the same directory or current directory.\n        import settings\n    except ImportError:\n        sys.stderr.write(\"Error: Can't find the file 'settings.py' in the directory containing %r \"\n                         \"or in the current directory. It appears you've customized things.\\n\"\n                         \"You'll have to run django-admin.py, passing it your settings module.\\n\"\n                         \"(If the file settings.py does indeed exist, it's causing an ImportError somehow.)\\n\"\n                         % __file__)\n        sys.exit(1)\n\n    from django.core import management\n\n    directory = management.setup_environ(settings)\n\n    if hasattr(management, 'color'):\n        # Current svn version of django\n        from django.core.management.color import color_style\n        style = color_style()\n    else:\n        # Compatibility with 0.96\n        from django.core.management import style\n\n    project_dir = os.path.normpath(os.path.join(directory, '..'))\n    parent_dir = os.path.basename(project_dir)\n    project_name = os.path.basename(directory)\n    if app_name == project_name:\n        sys.stderr.write(\n            style.ERROR(\n                'Error: You cannot create an app with the same name (%r) as your project.\\n' %\n                app_name))\n        sys.exit(1)\n    if app_name == 'facebook':\n        sys.stderr.write(style.ERROR(\n            'Error: You cannot name your app \"facebook\", '\n            'since this can cause conflicts with imports in Python < 2.5.\\n'))\n        sys.exit(1)\n    if not re.search(r'^\\w+$', app_name):\n        sys.stderr.write(\n            style.ERROR(\n                'Error: %r is not a valid app name. Please use only numbers, letters and underscores.\\n' %\n                app_name))\n        sys.exit(1)\n\n    top_dir = os.path.join(directory, app_name)\n    try:\n        os.mkdir(top_dir)\n    except OSError as e:\n        sys.stderr.write(style.ERROR(\"Error: %s\\n\" % e))\n        sys.exit(1)\n\n    import facebook\n\n    template_dir = os.path.join(\n        facebook.__path__[0],\n        'djangofb',\n        'default_app')\n\n    sys.stderr.write('Creating Facebook application %r...\\n' % app_name)\n\n    for d, subdirs, files in os.walk(template_dir):\n        relative_dir = d[len(template_dir) + 1:]\n        if relative_dir:\n            os.mkdir(os.path.join(top_dir, relative_dir))\n        subdirs[:] = [s for s in subdirs if not s.startswith('.')]\n        for f in files:\n            if f.endswith('.pyc'):\n                continue\n            path_old = os.path.join(d, f)\n            path_new = os.path.join(top_dir, relative_dir, f)\n            f_old = open(path_old, 'r')\n            f_new = open(path_new, 'w')\n            sys.stderr.write('Writing %s...\\n' % path_new)\n            f_new.write(\n                f_old.read().replace(\n                    '{{ project }}',\n                    project_name).replace(\n                    '{{ app }}',\n                    app_name))\n            f_new.close()\n            f_old.close()\n\n    sys.stderr.write('Done!\\n\\n')\n\n    from django.conf import settings\n\n    need_api_key = not hasattr(settings, 'FACEBOOK_API_KEY')\n    need_middleware = 'facebook.djangofb.FacebookMiddleware' not in settings.MIDDLEWARE_CLASSES\n    need_loader = 'django.template.loaders.app_directories.load_template_source' not in settings.TEMPLATE_LOADERS\n    need_install_app = not '%s.%s' % (\n        project_name, app_name) in settings.INSTALLED_APPS\n\n    if need_api_key or need_middleware or need_loader or need_install_app:\n        sys.stderr.write(\n            \"\"\"There are a couple of things you NEED to do before you can use this app:\\n\\n\"\"\")\n        if need_api_key:\n            sys.stderr.write(\n                \"\"\" * Set FACEBOOK_API_KEY and FACEBOOK_SECRET_KEY to the appropriate values in settings.py\\n\\n\"\"\")\n        if need_middleware:\n            sys.stderr.write(\n                \"\"\" * Add 'facebook.djangofb.FacebookMiddleware' to your MIDDLEWARE_CLASSES in settings.py\\n\\n\"\"\")\n        if need_loader:\n            sys.stderr.write(\n                \"\"\" * Add 'django.template.loaders.app_directories.load_template_source'\n                to your TEMPLATE_LOADERS in settings.py\\n\\n\"\"\")\n        if need_install_app:\n            sys.stderr.write(\n                \"\"\" * Add '%s.%s' to your INSTALLED_APPS in settings.py\\n\\n\"\"\" %\n                (project_name, app_name))\n\n    sys.stderr.write(\n        \"\"\"The final step is to add (r'^%s/', include('%s.%s.urls')) to your urls.py, and then set your callback page in the application settings on Facebook to 'http://your.domain.com/%s/'.\n\nGood luck!\n\n\"\"\" %\n        (project_name, project_name, app_name, project_name))\n"
  },
  {
    "path": "examples/fbsample/.project",
    "content": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<projectDescription>\r\n\t<name>fbsample</name>\r\n\t<comment></comment>\r\n\t<projects>\r\n\t</projects>\r\n\t<buildSpec>\r\n\t\t<buildCommand>\r\n\t\t\t<name>org.python.pydev.PyDevBuilder</name>\r\n\t\t\t<arguments>\r\n\t\t\t</arguments>\r\n\t\t</buildCommand>\r\n\t</buildSpec>\r\n\t<natures>\r\n\t\t<nature>org.python.pydev.pythonNature</nature>\r\n\t</natures>\r\n</projectDescription>\r\n"
  },
  {
    "path": "examples/fbsample/.pydevproject",
    "content": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\r\n<?eclipse-pydev version=\"1.0\"?>\r\n\r\n<pydev_project>\r\n<pydev_property name=\"org.python.pydev.PYTHON_PROJECT_VERSION\">python 2.6</pydev_property>\r\n<pydev_property name=\"org.python.pydev.PYTHON_PROJECT_INTERPRETER\">Default</pydev_property>\r\n</pydev_project>\r\n"
  },
  {
    "path": "examples/fbsample/__init__.py",
    "content": ""
  },
  {
    "path": "examples/fbsample/fbapp/__init__.py",
    "content": ""
  },
  {
    "path": "examples/fbsample/fbapp/models.py",
    "content": "from django.db import models\r\n\r\n# get_facebook_client lets us get the current Facebook object\r\n# from outside of a view, which lets us have cleaner code\r\nfrom facebook.djangofb import get_facebook_client\r\n\r\n\r\ndef _2int(d, k):\r\n    try:\r\n        d = d.__dict__\r\n    except:\r\n        pass\r\n\r\n    t = d.get(k, '')\r\n    if t == 'None':\r\n        t = 0\r\n    else:\r\n        t = int(t)\r\n    return t\r\n\r\n\r\nclass UserManager(models.Manager):\r\n    \"\"\"Custom manager for a Facebook User.\"\"\"\r\n\r\n    def get_current(self):\r\n        \"\"\"Gets a User object for the logged-in Facebook user.\"\"\"\r\n        facebook = get_facebook_client()\r\n        user, created = self.get_or_create(id=_2int(facebook, 'uid'))\r\n        if created:\r\n            # we could do some custom actions for new users here...\r\n            pass\r\n        return user\r\n\r\n\r\nclass User(models.Model):\r\n    \"\"\"A simple User model for Facebook users.\"\"\"\r\n\r\n    # We use the user's UID as the primary key in our database.\r\n    id = models.IntegerField(primary_key=True)\r\n\r\n    # TODO: The data that you want to store for each user would go here.\r\n    # For this sample, we let users let people know their favorite progamming\r\n    # language, in the spirit of Extended Info.\r\n    language = models.CharField(max_length=64, default='Python')\r\n\r\n    # Add the custom manager\r\n    objects = UserManager()\r\n"
  },
  {
    "path": "examples/fbsample/fbapp/templates/canvas.fbml",
    "content": "<fb:header>\r\n  {% comment %}\r\n    We can use {{ fbuser }} to get at the current user.\r\n    {{ fbuser.id }} will be the user's UID, and {{ fbuser.language }}\r\n    is his/her favorite language (Python :-).\r\n  {% endcomment %}\r\n  Welcome, <fb:name uid=\"{{ fbuser.id }}\" firstnameonly=\"true\" useyou=\"false\" />!\r\n</fb:header>\r\n\r\n<div class=\"clearfix\" style=\"float: left; border: 1px #d8dfea solid; padding: 10px 10px 10px 10px; margin-left: 30px; margin-bottom: 30px; width: 500px;\">\r\n  Your favorite language is {{ fbuser.language|escape }}.\r\n  <br /><br />\r\n\r\n  <div class=\"grayheader clearfix\">\r\n    <br /><br />\r\n\r\n    <form action=\".\" method=\"POST\">\r\n      <input type=\"text\" name=\"language\" value=\"{{ fbuser.language|escape }}\" />\r\n      <input type=\"submit\" value=\"Change\" />\r\n    </form>\r\n  </div>\r\n</div>\r\n"
  },
  {
    "path": "examples/fbsample/fbapp/urls.py",
    "content": "from django.conf.urls.defaults import *\r\n\r\nurlpatterns = patterns('fbsample.fbapp.views',\r\n                       (r'^$', 'canvas'),\r\n                       # Define other pages you want to create here\r\n                       )\r\n"
  },
  {
    "path": "examples/fbsample/fbapp/views.py",
    "content": "from django.http import HttpResponse\r\nfrom django.views.generic.simple import direct_to_template\r\n# uncomment the following two lines and the one below\r\n# if you don't want to use a decorator instead of the middleware\r\n# from django.utils.decorators import decorator_from_middleware\r\n# from facebook.djangofb import FacebookMiddleware\r\n\r\n# Import the Django helpers\r\nimport facebook.djangofb as facebook\r\n\r\n# The User model defined in models.py\r\nfrom models import User\r\n\r\n# We'll require login for our canvas page. This\r\n# isn't necessarily a good idea, as we might want\r\n# to let users see the page without granting our app\r\n# access to their info. See the wiki for details on how\r\n# to do this.\r\n# @decorator_from_middleware(FacebookMiddleware)\r\n\r\n\r\n@facebook.require_login()\r\ndef canvas(request):\r\n    # Get the User object for the currently logged in user\r\n    user = User.objects.get_current()\r\n\r\n    # Check if we were POSTed the user's new language of choice\r\n    if 'language' in request.POST:\r\n        user.language = request.POST['language'][:64]\r\n        user.save()\r\n\r\n    # User is guaranteed to be logged in, so pass canvas.fbml\r\n    # an extra 'fbuser' parameter that is the User object for\r\n    # the currently logged in user.\r\n    return direct_to_template(\r\n        request,\r\n        'canvas.fbml',\r\n        extra_context={\r\n            'fbuser': user})\r\n\r\n\r\n@facebook.require_login()\r\ndef ajax(request):\r\n    return HttpResponse('hello world')\r\n"
  },
  {
    "path": "examples/fbsample/manage.py",
    "content": "#!/usr/bin/env python\r\nfrom django.core.management import execute_manager\r\ntry:\r\n    import settings  # Assumed to be in the same directory.\r\nexcept ImportError:\r\n    import sys\r\n    sys.stderr.write(\"Error: Can't find the file 'settings.py' in the directory containing %r. \"\r\n                     \"It appears you've customized things.\\nYou'll have to run django-admin.py, \"\r\n                     \"passing it your settings module.\\n(If the file settings.py does indeed exist, \"\r\n                     \"it's causing an ImportError somehow.)\\n\" % __file__)\r\n    sys.exit(1)\r\n\r\nif __name__ == \"__main__\":\r\n    execute_manager(settings)\r\n"
  },
  {
    "path": "examples/fbsample/run.bat",
    "content": "python manage.py runserver 0.0.0.0:80"
  },
  {
    "path": "examples/fbsample/settings.py",
    "content": "# Django settings for fbsample project.\r\n\r\nDEBUG = True\r\nTEMPLATE_DEBUG = DEBUG\r\n\r\nADMINS = (\r\n    # ('Your Name', 'your_email@domain.com'),\r\n)\r\n\r\nMANAGERS = ADMINS\r\n\r\n# 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.\r\nDATABASE_ENGINE = 'sqlite3'\r\n# Or path to database file if using sqlite3.\r\nDATABASE_NAME = 'db.sqlite3'\r\nDATABASE_USER = ''             # Not used with sqlite3.\r\nDATABASE_PASSWORD = ''         # Not used with sqlite3.\r\n# Set to empty string for localhost. Not used with sqlite3.\r\nDATABASE_HOST = ''\r\n# Set to empty string for default. Not used with sqlite3.\r\nDATABASE_PORT = ''\r\n\r\n# Local time zone for this installation. Choices can be found here:\r\n# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name\r\n# although not all choices may be available on all operating systems.\r\n# If running in a Windows environment this must be set to the same as your\r\n# system time zone.\r\nTIME_ZONE = 'America/Chicago'\r\n\r\n# Language code for this installation. All choices can be found here:\r\n# http://www.i18nguy.com/unicode/language-identifiers.html\r\nLANGUAGE_CODE = 'en-us'\r\n\r\nSITE_ID = 1\r\n\r\n# If you set this to False, Django will make some optimizations so as not\r\n# to load the internationalization machinery.\r\nUSE_I18N = True\r\n\r\n# Absolute path to the directory that holds media.\r\n# Example: \"/home/media/media.lawrence.com/\"\r\nMEDIA_ROOT = ''\r\n\r\n# URL that handles the media served from MEDIA_ROOT. Make sure to use a\r\n# trailing slash if there is a path component (optional in other cases).\r\n# Examples: \"http://media.lawrence.com\", \"http://example.com/media/\"\r\nMEDIA_URL = ''\r\n\r\n# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a\r\n# trailing slash.\r\n# Examples: \"http://foo.com/media/\", \"/media/\".\r\nADMIN_MEDIA_PREFIX = '/media/'\r\n\r\n# Make this unique, and don't share it with anybody.\r\nSECRET_KEY = 'yg6zh@+u^w3agtjwy^da)#277d3j#a%3m@)pev8_j0ozztwe4+'\r\n\r\n# List of callables that know how to import templates from various sources.\r\nTEMPLATE_LOADERS = (\r\n    'django.template.loaders.filesystem.load_template_source',\r\n    'django.template.loaders.app_directories.load_template_source',\r\n    #     'django.template.loaders.eggs.load_template_source',\r\n)\r\n\r\nMIDDLEWARE_CLASSES = (\r\n    'django.middleware.common.CommonMiddleware',\r\n    'django.contrib.sessions.middleware.SessionMiddleware',\r\n    'django.contrib.auth.middleware.AuthenticationMiddleware',\r\n\r\n    'facebook.djangofb.FacebookMiddleware',\r\n)\r\n\r\nROOT_URLCONF = 'fbsample.urls'\r\n\r\nTEMPLATE_DIRS = (\r\n    # Put strings here, like \"/home/html/django_templates\" or \"C:/www/django/templates\".\r\n    # Always use forward slashes, even on Windows.\r\n    # Don't forget to use absolute paths, not relative paths.\r\n)\r\n\r\nINSTALLED_APPS = (\r\n    'django.contrib.auth',\r\n    'django.contrib.contenttypes',\r\n    'django.contrib.sessions',\r\n    'django.contrib.sites',\r\n\r\n    'fbsample.fbapp',\r\n)\r\n\r\n# get it from here\r\n# http://www.facebook.com/editapps.php?ref=mb\r\nFACEBOOK_API_KEY = 'x'\r\nFACEBOOK_SECRET_KEY = 'xx'\r\n"
  },
  {
    "path": "examples/fbsample/urls.py",
    "content": "from django.conf.urls.defaults import *\r\n\r\n# Uncomment the next two lines to enable the admin:\r\n# from django.contrib import admin\r\n# admin.autodiscover()\r\n\r\nurlpatterns = patterns('',\r\n                       # Example:\r\n                       # (r'^fbsample/', include('fbsample.foo.urls')),\r\n\r\n                       # Uncomment the admin/doc line below and add 'django.contrib.admindocs'\r\n                       # to INSTALLED_APPS to enable admin documentation:\r\n                       # (r'^admin/doc/', include('django.contrib.admindocs.urls')),\r\n\r\n                       # Uncomment the next line to enable the admin:\r\n                       # (r'^admin/', include(admin.site.urls)),\r\n\r\n                       (r'^fbsample/', include('fbsample.fbapp.urls')),\r\n                       )\r\n"
  },
  {
    "path": "facebook/__init__.py",
    "content": "#! /usr/bin/env python\n#\n# pyfacebook - Python bindings for the Facebook API\n#\n# Copyright (c) 2008, Samuel Cormier-Iijima\n# All rights reserved.\n#\n# Redistribution and use in source and binary forms, with or without\n# modification, are permitted provided that the following conditions are met:\n#     * Redistributions of source code must retain the above copyright\n#       notice, this list of conditions and the following disclaimer.\n#     * Redistributions in binary form must reproduce the above copyright\n#       notice, this list of conditions and the following disclaimer in the\n#       documentation and/or other materials provided with the distribution.\n#     * Neither the name of the author nor the names of its contributors may\n#       be used to endorse or promote products derived from this software\n#       without specific prior written permission.\n#\n# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS``AS IS'' AND ANY\n# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\n# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\n# DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY\n# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\n# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\n# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\n# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\n# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n\n\"\"\"\nPython bindings for the Facebook API (pyfacebook - http://code.google.com/p/pyfacebook)\n\nPyFacebook is a client library that wraps the Facebook API.\n\nFor more information, see\n\nHome Page: http://code.google.com/p/pyfacebook\nDeveloper Wiki: http://wiki.developers.facebook.com/index.php/Python\nFacebook IRC Channel: #facebook on irc.freenode.net\n\nPyFacebook can use simplejson if it is installed, which\nis much faster than XML and also uses less bandwith. Go to\nhttp://undefined.org/python/#simplejson to download it, or do\napt-get install python-simplejson on a Debian-like system.\n\"\"\"\n\nimport sys\nimport time\nimport struct\nimport urllib\nimport urllib2\nimport httplib\nimport hmac\ntry:\n    import hashlib\nexcept ImportError:\n    import md5 as hashlib\nimport binascii\nimport urlparse\nimport mimetypes\n\n# try to use simplejson first, otherwise fallback to XML\nRESPONSE_FORMAT = 'JSON'\ntry:\n    import json as simplejson\n    simplejson.loads\nexcept (ImportError, AttributeError):\n    try:\n        import simplejson\n        simplejson.loads\n    except (ImportError, AttributeError):\n        try:\n            from django.utils import simplejson\n            simplejson.loads\n        except (ImportError, AttributeError):\n            try:\n                import jsonlib as simplejson\n                simplejson.loads\n            except (ImportError, AttributeError):\n                from xml.dom import minidom\n                RESPONSE_FORMAT = 'XML'\n\n# support Google App Engine.  GAE does not have a working urllib.urlopen.\ntry:\n    from google.appengine.api import urlfetch\n\n    def urlread(url, data=None, headers=None):\n        if data is not None:\n            if headers is None:\n                headers = {\"Content-type\": \"application/x-www-form-urlencoded\"}\n            method = urlfetch.POST\n        else:\n            if headers is None:\n                headers = {}\n            method = urlfetch.GET\n\n        result = urlfetch.fetch(url, method=method,\n                                payload=data, headers=headers)\n\n        if result.status_code == 200:\n            return result.content\n        else:\n            raise urllib2.URLError(\n                \"fetch error url=%s, code=%d\" %\n                (url, result.status_code))\n\nexcept ImportError:\n    def urlread(url, data=None):\n        res = urllib2.urlopen(url, data=data)\n        return res.read()\n\n__all__ = ['Facebook', 'create_hmac']\n\nVERSION = '1.0a2'\n\nFACEBOOK_URL = 'http://api.facebook.com/restserver.php'\nFACEBOOK_VIDEO_URL = 'http://api-video.facebook.com/restserver.php'\nFACEBOOK_SECURE_URL = 'https://api.facebook.com/restserver.php'\n\n\ndef create_hmac(key, tbhashed):\n    return hmac.new(key, tbhashed, hashlib.sha1).hexdigest()\n\n\nclass json(object):\n    pass\n\n# simple IDL for the Facebook API\nMETHODS = {\n    # dashboard methods\n    'dashboard': {\n        'incrementCount': [\n            ('uid', int, ['optional'])\n        ],\n        'decrementCount': [\n            ('uid', int, ['optional'])\n        ],\n        'getCount': [\n            ('uid', int, ['optional'])\n        ],\n        'setCount': [\n            ('count', int, []),\n            ('uid', int, ['optional'])\n        ],\n        'getActivity': [\n            ('activity_ids', list, ['optional']),\n            ('uid', int, ['optional'])\n        ],\n        'publishActivity': [\n            ('activity', json, []),\n\n        ]\n\n    },\n    'application': {\n        'getPublicInfo': [\n            ('application_id', int, ['optional']),\n            ('application_api_key', str, ['optional']),\n            ('application_canvas_name', str, ['optional']),\n        ],\n    },\n\n    # admin methods\n    'admin': {\n        'getAllocation': [\n            ('integration_point_name', str, []),\n        ],\n        'getRestrictionInfo': [],\n        'setRestrictionInfo': [\n            ('format', str, ['optional']),\n            ('callback', str, ['optional']),\n            ('restriction_str', json, ['optional']),\n        ],\n        # Some methods don't work with access_tokens, the signed option forces\n        # use of the secret_key signature (avoids error 15 and, sometimes, 8)\n        'getAppProperties': [\n            ('properties', list, []),\n            'signed'\n        ],\n        'setAppProperties': [\n            ('properties', json, []),\n            'signed'\n        ],\n    },\n\n    # auth methods\n    'auth': {\n        'revokeAuthorization': [\n            ('uid', int, ['optional']),\n        ],\n        'revokeExtendedPermission': [\n            ('perm', str, []),\n            ('uid', int, ['optional']),\n        ],\n    },\n\n    # feed methods\n    'feed': {\n        'publishStoryToUser': [\n            ('title', str, []),\n            ('body', str, ['optional']),\n            ('image_1', str, ['optional']),\n            ('image_1_link', str, ['optional']),\n            ('image_2', str, ['optional']),\n            ('image_2_link', str, ['optional']),\n            ('image_3', str, ['optional']),\n            ('image_3_link', str, ['optional']),\n            ('image_4', str, ['optional']),\n            ('image_4_link', str, ['optional']),\n            ('priority', int, ['optional']),\n        ],\n\n        'publishActionOfUser': [\n            ('title', str, []),\n            ('body', str, ['optional']),\n            ('image_1', str, ['optional']),\n            ('image_1_link', str, ['optional']),\n            ('image_2', str, ['optional']),\n            ('image_2_link', str, ['optional']),\n            ('image_3', str, ['optional']),\n            ('image_3_link', str, ['optional']),\n            ('image_4', str, ['optional']),\n            ('image_4_link', str, ['optional']),\n            ('priority', int, ['optional']),\n        ],\n\n        'publishTemplatizedAction': [\n            ('title_template', str, []),\n            ('page_actor_id', int, ['optional']),\n            ('title_data', json, ['optional']),\n            ('body_template', str, ['optional']),\n            ('body_data', json, ['optional']),\n            ('body_general', str, ['optional']),\n            ('image_1', str, ['optional']),\n            ('image_1_link', str, ['optional']),\n            ('image_2', str, ['optional']),\n            ('image_2_link', str, ['optional']),\n            ('image_3', str, ['optional']),\n            ('image_3_link', str, ['optional']),\n            ('image_4', str, ['optional']),\n            ('image_4_link', str, ['optional']),\n            ('target_ids', list, ['optional']),\n        ],\n\n        'registerTemplateBundle': [\n            ('one_line_story_templates', json, []),\n            ('short_story_templates', json, ['optional']),\n            ('full_story_template', json, ['optional']),\n            ('action_links', json, ['optional']),\n        ],\n\n        'deactivateTemplateBundleByID': [\n            ('template_bundle_id', int, []),\n        ],\n\n        'getRegisteredTemplateBundles': [],\n\n        'getRegisteredTemplateBundleByID': [\n            ('template_bundle_id', str, []),\n        ],\n\n        'publishUserAction': [\n            ('template_bundle_id', int, []),\n            ('template_data', json, ['optional']),\n            ('target_ids', list, ['optional']),\n            ('body_general', str, ['optional']),\n            ('story_size', int, ['optional']),\n        ],\n    },\n\n    # fql methods\n    'fql': {\n        'query': [\n            ('query', str, []),\n        ],\n        'multiquery': [\n            ('queries', json, []),\n        ],\n    },\n\n    # friends methods\n    'friends': {\n        'areFriends': [\n            ('uids1', list, []),\n            ('uids2', list, []),\n        ],\n\n        'get': [\n            ('flid', int, ['optional']),\n        ],\n\n        'getLists': [],\n\n        'getAppUsers': [],\n\n        'getMutualFriends': [\n            ('target_uid', int, []),\n            ('source_uid', int, ['optional']),\n        ],\n    },\n\n    # notifications methods\n    'notifications': {\n        'get': [],\n\n        'send': [\n            ('to_ids', list, []),\n            ('notification', str, []),\n            ('email', str, ['optional']),\n            ('type', str, ['optional']),\n        ],\n\n        'sendRequest': [\n            ('to_ids', list, []),\n            ('type', str, []),\n            ('content', str, []),\n            ('image', str, []),\n            ('invite', bool, []),\n        ],\n\n        'sendEmail': [\n            ('recipients', list, []),\n            ('subject', str, []),\n            ('text', str, ['optional']),\n            ('fbml', str, ['optional']),\n        ]\n    },\n\n    # profile methods\n    'profile': {\n        'setFBML': [\n            ('markup', str, ['optional']),\n            ('uid', int, ['optional']),\n            ('profile', str, ['optional']),\n            ('profile_action', str, ['optional']),\n            ('mobile_fbml', str, ['optional']),\n            ('profile_main', str, ['optional']),\n        ],\n\n        'getFBML': [\n            ('uid', int, ['optional']),\n            ('type', int, ['optional']),\n        ],\n\n        'setInfo': [\n            ('title', str, []),\n            ('type', int, []),\n            ('info_fields', json, []),\n            ('uid', int, []),\n        ],\n\n        'getInfo': [\n            ('uid', int, []),\n        ],\n\n        'setInfoOptions': [\n            ('field', str, []),\n            ('options', json, []),\n        ],\n\n        'getInfoOptions': [\n            ('field', str, []),\n        ],\n    },\n\n    # users methods\n    'users': {\n        'getInfo': [\n            ('uids', list, []),\n            ('fields', list, [('default', ['name'])]),\n        ],\n\n        'getStandardInfo': [\n            ('uids', list, []),\n            ('fields', list, [('default', ['uid'])]),\n        ],\n\n        'getLoggedInUser': [],\n\n        'isAppAdded': [],\n\n        'isAppUser': [\n            ('uid', int, []),\n        ],\n\n        'hasAppPermission': [\n            ('ext_perm', str, []),\n            ('uid', int, ['optional']),\n        ],\n\n        'setStatus': [\n            ('status', str, []),\n            ('clear', bool, []),\n            ('status_includes_verb', bool, ['optional']),\n            ('uid', int, ['optional']),\n        ],\n    },\n\n    # events methods\n    'events': {\n        'cancel': [\n            ('eid', int, []),\n            ('cancel_message', str, ['optional']),\n        ],\n\n        'create': [\n            ('event_info', json, []),\n        ],\n\n        'edit': [\n            ('eid', int, []),\n            ('event_info', json, []),\n        ],\n\n        'get': [\n            ('uid', int, ['optional']),\n            ('eids', list, ['optional']),\n            ('start_time', int, ['optional']),\n            ('end_time', int, ['optional']),\n            ('rsvp_status', str, ['optional']),\n        ],\n\n        'getMembers': [\n            ('eid', int, []),\n        ],\n\n        'invite': [\n            ('eid', int, []),\n            ('uids', list, []),\n            ('personal_message', str, ['optional']),\n        ],\n\n        'rsvp': [\n            ('eid', int, []),\n            ('rsvp_status', str, []),\n        ],\n\n        'edit': [\n            ('eid', int, []),\n            ('event_info', json, []),\n        ],\n\n        'invite': [\n            ('eid', int, []),\n            ('uids', list, []),\n            ('personal_message', str, ['optional']),\n        ],\n    },\n\n    # update methods\n    'update': {\n        'decodeIDs': [\n            ('ids', list, []),\n        ],\n    },\n\n    # groups methods\n    'groups': {\n        'get': [\n            ('uid', int, ['optional']),\n            ('gids', list, ['optional']),\n        ],\n\n        'getMembers': [\n            ('gid', int, []),\n        ],\n    },\n\n    # marketplace methods\n    'marketplace': {\n        'createListing': [\n            ('listing_id', int, []),\n            ('show_on_profile', bool, []),\n            ('listing_attrs', str, []),\n        ],\n\n        'getCategories': [],\n\n        'getListings': [\n            ('listing_ids', list, []),\n            ('uids', list, []),\n        ],\n\n        'getSubCategories': [\n            ('category', str, []),\n        ],\n\n        'removeListing': [\n            ('listing_id', int, []),\n            ('status', str, []),\n        ],\n\n        'search': [\n            ('category', str, ['optional']),\n            ('subcategory', str, ['optional']),\n            ('query', str, ['optional']),\n        ],\n    },\n\n    # pages methods\n    'pages': {\n        'getInfo': [\n            ('fields', list, [('default', ['page_id', 'name'])]),\n            ('page_ids', list, ['optional']),\n            ('uid', int, ['optional']),\n        ],\n\n        'isAdmin': [\n            ('page_id', int, []),\n        ],\n\n        'isAppAdded': [\n            ('page_id', int, []),\n        ],\n\n        'isFan': [\n            ('page_id', int, []),\n            ('uid', int, []),\n        ],\n    },\n\n    # photos methods\n    'photos': {\n        'addTag': [\n            ('pid', int, []),\n            ('tag_uid', int, [('default', 0)]),\n            ('tag_text', str, [('default', '')]),\n            ('x', float, [('default', 50)]),\n            ('y', float, [('default', 50)]),\n            ('tags', json, ['optional']),\n        ],\n\n        'createAlbum': [\n            ('name', str, []),\n            ('location', str, ['optional']),\n            ('description', str, ['optional']),\n        ],\n\n        'get': [\n            ('subj_id', int, ['optional']),\n            ('aid', int, ['optional']),\n            ('pids', list, ['optional']),\n        ],\n\n        'getAlbums': [\n            ('uid', int, ['optional']),\n            ('aids', list, ['optional']),\n        ],\n\n        'getTags': [\n            ('pids', list, []),\n        ],\n    },\n\n    # videos methods\n    'video': {\n        'getUploadLimits': [\n        ],\n    },\n\n    # status methods\n    'status': {\n        'get': [\n            ('uid', int, ['optional']),\n            ('limit', int, ['optional']),\n        ],\n        'set': [\n            ('status', str, ['optional']),\n            ('uid', int, ['optional']),\n        ],\n    },\n\n    # fbml methods\n    'fbml': {\n        'refreshImgSrc': [\n            ('url', str, []),\n        ],\n\n        'refreshRefUrl': [\n            ('url', str, []),\n        ],\n\n        'setRefHandle': [\n            ('handle', str, []),\n            ('fbml', str, []),\n        ],\n    },\n\n    # SMS Methods\n    'sms': {\n        'canSend': [\n            ('uid', int, []),\n        ],\n\n        'send': [\n            ('uid', int, []),\n            ('message', str, []),\n            ('session_id', int, []),\n            ('req_session', bool, []),\n        ],\n    },\n\n    'data': {\n        'getCookies': [\n            ('uid', int, []),\n            ('string', str, ['optional']),\n        ],\n\n        'setCookie': [\n            ('uid', int, []),\n            ('name', str, []),\n            ('value', str, []),\n            ('expires', int, ['optional']),\n            ('path', str, ['optional']),\n        ],\n    },\n\n    # connect methods\n    'connect': {\n        'registerUsers': [\n            ('accounts', json, []),\n        ],\n\n        'unregisterUsers': [\n            ('email_hashes', json, []),\n        ],\n\n        'getUnconnectedFriendsCount': [\n        ],\n    },\n\n    'links': {\n        'post': [\n            ('url', str, []),\n            ('comment', str, []),\n            ('uid', int, []),\n            ('image', str, ['optional']),\n            ('callback', str, ['optional']),\n        ],\n        'preview': [\n            ('url', str, []),\n            ('callback', str, ['optional']),\n        ],\n    },\n\n    # stream methods (beta)\n    'stream': {\n        'addComment': [\n            ('post_id', int, []),\n            ('comment', str, []),\n            ('uid', int, ['optional']),\n        ],\n\n        'addLike': [\n            ('uid', int, ['optional']),\n            ('post_id', int, ['optional']),\n        ],\n\n        'get': [\n            ('viewer_id', int, ['optional']),\n            ('source_ids', list, ['optional']),\n            ('start_time', int, ['optional']),\n            ('end_time', int, ['optional']),\n            ('limit', int, ['optional']),\n            ('filter_key', str, ['optional']),\n        ],\n\n        'getComments': [\n            ('post_id', int, []),\n        ],\n\n        'getFilters': [\n            ('uid', int, ['optional']),\n        ],\n\n        'publish': [\n            ('message', str, ['optional']),\n            ('attachment', json, ['optional']),\n            ('action_links', json, ['optional']),\n            ('target_id', str, ['optional']),\n            ('uid', str, ['optional']),\n        ],\n\n        'remove': [\n            ('post_id', int, []),\n            ('uid', int, ['optional']),\n        ],\n\n        'removeComment': [\n            ('comment_id', int, []),\n            ('uid', int, ['optional']),\n        ],\n\n        'removeLike': [\n            ('uid', int, ['optional']),\n            ('post_id', int, ['optional']),\n        ],\n    },\n\n    # livemessage methods (beta)\n    'livemessage': {\n        'send': [\n            ('recipient', int, []),\n            ('event_name', str, []),\n            ('message', str, []),\n        ],\n    },\n\n    # dashboard methods (beta)\n    'dashboard': {\n        # setting counters for a single user\n        'decrementCount': [\n            ('uid', int, []),\n        ],\n\n        'incrementCount': [\n            ('uid', int, []),\n        ],\n\n        'getCount': [\n            ('uid', int, []),\n        ],\n\n        'setCount': [\n            ('uid', int, []),\n            ('count', int, []),\n        ],\n\n        # setting counters for multiple users\n        'multiDecrementCount': [\n            ('uids', list, []),\n        ],\n\n        'multiIncrementCount': [\n            ('uids', list, []),\n        ],\n\n        'multiGetCount': [\n            ('uids', list, []),\n        ],\n\n        'multiSetCount': [\n            ('uids', list, []),\n        ],\n\n        # setting news for a single user\n        'addNews': [\n            ('news', json, []),\n            ('image', str, ['optional']),\n            ('uid', int, ['optional']),\n        ],\n\n        'getNews': [\n            ('uid', int, []),\n            ('news_ids', list, ['optional']),\n        ],\n\n        'clearNews': [\n            ('uid', int, []),\n            ('news_ids', list, ['optional']),\n        ],\n\n        # setting news for multiple users\n        'multiAddNews': [\n            ('uids', list, []),\n            ('news', json, []),\n            ('image', str, ['optional']),\n        ],\n\n        'multiGetNews': [\n            ('uids', json, []),\n        ],\n\n        'multiClearNews': [\n            ('uids', json, []),\n        ],\n\n        # setting application news for all users\n        'addGlobalNews': [\n            ('news', json, []),\n            ('image', str, ['optional']),\n        ],\n\n        'getGlobalNews': [\n            ('news_ids', list, ['optional']),\n        ],\n\n        'clearGlobalNews': [\n            ('news_ids', list, ['optional']),\n        ],\n\n        # user activity\n        'getActivity': [\n            ('activity_ids', list, ['optional']),\n        ],\n\n        'publishActivity': [\n            ('activity', json, []),\n        ],\n\n        'removeActivity': [\n            ('activity_ids', list, []),\n        ],\n    },\n\n    # comments methods (beta)\n    'comments': {\n        'add': [\n            # text should be after xid/object_id, but is required\n            ('text', str, []),\n            # One of xid and object_is is required. Can this be expressed?\n            ('xid', str, ['optional']),\n            ('object_id', str, ['optional']),\n            ('uid', int, ['optional']),\n            ('title', str, ['optional']),\n            ('url', str, ['optional']),\n            ('publish_to_stream', bool, [('default', False)]),\n        ],\n\n        'remove': [\n            # One of xid and object_is is required. Can this be expressed?\n            ('xid', str, ['optional']),\n            ('object_id', str, ['optional']),\n            # comment_id should be required\n            ('comment_id', str, ['optional']),\n        ],\n\n        'get': [\n            # One of xid and object_is is required. Can this be expressed?\n            ('xid', str, ['optional']),\n            ('object_id', str, ['optional']),\n        ],\n    }\n}\n\n\ndef __fixup_param(name, klass, options, param):\n    optional = 'optional' in options\n    default = [x[1] for x in options if isinstance(x, tuple) and x[\n        0] == 'default']\n    if default:\n        default = default[0]\n    else:\n        default = None\n    if param is None:\n        if klass is list and default:\n            param = default[:]\n        else:\n            param = default\n    if klass is json and isinstance(param, (list, dict)):\n        param = simplejson.dumps(param)\n    return param\n\n\ndef __generate_facebook_method(namespace, method_name, param_data):\n    # This method-level option forces the method to be signed rather than using\n    # the access_token\n    signed = False\n    if 'signed' in param_data:\n        signed = True\n        param_data.remove('signed')\n\n    # a required parameter doesn't have 'optional' in the options,\n    # and has no tuple option that starts with 'default'\n    required = [x for x in param_data if 'optional' not in x[2] and not [\n        y for y in x[2] if isinstance(y, tuple) and y and y[0] == 'default']]\n\n    def facebook_method(self, *args, **kwargs):\n        params = {}\n        for i, arg in enumerate(args):\n            params[param_data[i][0]] = arg\n        params.update(kwargs)\n\n        for param in required:\n            if param[0] not in params:\n                raise TypeError(\"missing parameter %s\" % param[0])\n\n        for name, klass, options in param_data:\n            if name in params:\n                params[name] = __fixup_param(\n                    name, klass, options, params[name])\n\n        return self(method_name, params, signed=signed)\n\n    facebook_method.__name__ = method_name\n    facebook_method.__doc__ = \"Facebook API call. See http://developers.facebook.com/documentation.php?v=1.0&method=%s.%s\" % (\n        namespace, method_name)\n\n    return facebook_method\n\n\nclass Proxy(object):\n    \"\"\"Represents a \"namespace\" of Facebook API calls.\"\"\"\n\n    def __init__(self, client, name):\n        self._client = client\n        self._name = name\n\n    def __call__(\n            self,\n            method=None,\n            args=None,\n            add_session_args=True,\n            signed=False):\n        # for Django templates\n        if method is None:\n            return self\n\n        if add_session_args:\n            self._client._add_session_args(args)\n\n        return self._client(\n            '%s.%s' %\n            (self._name, method), args, signed=signed)\n\n\n# generate the Facebook proxies\ndef __generate_proxies():\n    for namespace in METHODS:\n        methods = {}\n\n        for method, param_data in METHODS[namespace].iteritems():\n            methods[method] = __generate_facebook_method(\n                namespace, method, param_data)\n\n        proxy = type('%sProxy' % namespace.title(), (Proxy,), methods)\n\n        globals()[proxy.__name__] = proxy\n\n__generate_proxies()\n\n\nclass FacebookError(Exception):\n    \"\"\"Exception class for errors received from Facebook.\"\"\"\n\n    def __init__(self, code, msg, args=None):\n        self.code = code\n        self.msg = msg\n        self.extra_args = args\n        Exception.__init__(self, code, msg, args)\n\n    def __str__(self):\n        return 'Error %s: %s' % (self.code, self.msg)\n\n\nclass AuthProxy(AuthProxy):\n    \"\"\"Special proxy for facebook.auth.\"\"\"\n\n    def getSession(self):\n        \"\"\"Facebook API call. See http://developers.facebook.com/documentation.php?v=1.0&method=auth.getSession\"\"\"\n        args = {}\n        try:\n            args['auth_token'] = self._client.auth_token\n        except AttributeError:\n            raise RuntimeError('Client does not have auth_token set.')\n        try:\n            args['generate_session_secret'] = self._client.generate_session_secret\n        except AttributeError:\n            pass\n        result = self._client('%s.getSession' % self._name, args)\n        self._client.session_key = result['session_key']\n        self._client.uid = result['uid']\n        self._client.secret = result.get('secret')\n        self._client.session_key_expires = result['expires']\n        return result\n\n    def createToken(self):\n        \"\"\"Facebook API call. See http://developers.facebook.com/documentation.php?v=1.0&method=auth.createToken\"\"\"\n        token = self._client('%s.createToken' % self._name)\n        self._client.auth_token = token\n        return token\n\n\nclass FriendsProxy(FriendsProxy):\n    \"\"\"Special proxy for facebook.friends.\"\"\"\n\n    def get(self, **kwargs):\n        \"\"\"Facebook API call. See http://developers.facebook.com/documentation.php?v=1.0&method=friends.get\"\"\"\n        if not kwargs.get('flid') and self._client._friends:\n            return self._client._friends\n        return super(FriendsProxy, self).get(**kwargs)\n\n\nclass PhotosProxy(PhotosProxy):\n    \"\"\"Special proxy for facebook.photos.\"\"\"\n\n    def upload(\n            self,\n            image,\n            aid=None,\n            uid=None,\n            caption=None,\n            size=(\n                720,\n                720),\n            filename=None,\n            callback=None):\n        \"\"\"Facebook API call. See http://developers.facebook.com/documentation.php?v=1.0&method=photos.upload\n\n        size -- an optional size (width, height) to resize the image to before uploading. Resizes by default\n                to Facebook's maximum display dimension of 720.\n        \"\"\"\n        args = {}\n\n        if uid is not None:\n            args['uid'] = uid\n\n        if aid is not None:\n            args['aid'] = aid\n\n        if caption is not None:\n            args['caption'] = caption\n\n        if self._client.oauth2:\n            url = self._client.facebook_secure_url\n        else:\n            url = self._client.facebook_url\n\n        args = self._client._build_post_args(\n            'facebook.photos.upload',\n            self._client._add_session_args(args))\n\n        try:\n            import cStringIO as StringIO\n        except ImportError:\n            import StringIO\n\n        # check for a filename specified...if the user is passing binary data in\n        # image then a filename will be specified\n        if filename is None:\n            try:\n                import Image\n            except ImportError:\n                data = StringIO.StringIO(open(image, 'rb').read())\n            else:\n                img = Image.open(image)\n                if size:\n                    img.thumbnail(size, Image.ANTIALIAS)\n                data = StringIO.StringIO()\n                img.save(data, img.format)\n        else:\n            # there was a filename specified, which indicates that image was not\n            # the path to an image file but rather the binary data of a file\n            data = StringIO.StringIO(image)\n            image = filename\n\n        content_type, body = self.__encode_multipart_formdata(\n            list(args.iteritems()), [(image, data)])\n        urlinfo = urlparse.urlsplit(url)\n        try:\n            content_length = len(body)\n            chunk_size = 4096\n\n            if self._client.oauth2:\n                h = httplib.HTTPSConnection(urlinfo[1])\n            else:\n                h = httplib.HTTPConnection(urlinfo[1])\n            h.putrequest('POST', urlinfo[2])\n            h.putheader('Content-Type', content_type)\n            h.putheader('Content-Length', str(content_length))\n            h.putheader('MIME-Version', '1.0')\n            h.putheader('User-Agent', 'PyFacebook Client Library')\n            h.endheaders()\n\n            if callback:\n                count = 0\n                while len(body) > 0:\n                    if len(body) < chunk_size:\n                        data = body\n                        body = ''\n                    else:\n                        data = body[0:chunk_size]\n                        body = body[chunk_size:]\n\n                    h.send(data)\n                    count += 1\n                    callback(count, chunk_size, content_length)\n            else:\n                h.send(body)\n\n            response = h.getresponse()\n\n            if response.status != 200:\n                raise Exception(\n                    'Error uploading photo: Facebook returned HTTP %s (%s)' %\n                    (response.status, response.reason))\n            response = response.read()\n        except:\n            # sending the photo failed, perhaps we are using GAE\n            try:\n                from google.appengine.api import urlfetch\n\n                try:\n                    response = urlread(\n                        url=url,\n                        data=body,\n                        headers={\n                            'POST': urlinfo[2],\n                            'Content-Type': content_type,\n                            'MIME-Version': '1.0'})\n                except urllib2.URLError:\n                    raise Exception(\n                        'Error uploading photo: Facebook returned %s' %\n                        (response))\n            except ImportError:\n                # could not import from google.appengine.api, so we are not\n                # running in GAE\n                raise Exception('Error uploading photo.')\n\n        return self._client._parse_response(response, 'facebook.photos.upload')\n\n    def __encode_multipart_formdata(self, fields, files):\n        \"\"\"Encodes a multipart/form-data message to upload an image.\"\"\"\n        boundary = '-------tHISiStheMulTIFoRMbOUNDaRY'\n        crlf = '\\r\\n'\n        l = []\n\n        for (key, value) in fields:\n            l.append('--' + boundary)\n            l.append('Content-Disposition: form-data; name=\"%s\"' % str(key))\n            l.append('')\n            l.append(str(value))\n        for (filename, value) in files:\n            l.append('--' + boundary)\n            l.append(\n                'Content-Disposition: form-data; filename=\"%s\"' %\n                (str(filename), ))\n            l.append('Content-Type: %s' % self.__get_content_type(filename))\n            l.append('')\n            l.append(value.getvalue())\n        l.append('--' + boundary + '--')\n        l.append('')\n        body = crlf.join(l)\n        content_type = 'multipart/form-data; boundary=%s' % boundary\n        return content_type, body\n\n    def __get_content_type(self, filename):\n        \"\"\"Returns a guess at the MIME type of the file from the filename.\"\"\"\n        return str(mimetypes.guess_type(filename)[\n                   0]) or 'application/octet-stream'\n\n\nclass VideoProxy(VideoProxy):\n    \"\"\"Special proxy for facebook.video.\"\"\"\n\n    def upload(\n            self,\n            video,\n            aid=None,\n            title=None,\n            description=None,\n            filename=None,\n            uid=None,\n            privacy=None,\n            callback=None):\n        \"\"\"Facebook API call. See http://wiki.developers.facebook.com/index.php/Video.upload\"\"\"\n        args = {}\n\n        if aid is not None:\n            args['aid'] = aid\n\n        if title is not None:\n            args['title'] = title\n\n        if description is not None:\n            args['description'] = description\n\n        if uid is not None:\n            args['uid'] = uid\n\n        if privacy is not None:\n            args['privacy'] = privacy\n\n        args = self._client._build_post_args(\n            'facebook.video.upload',\n            self._client._add_session_args(args))\n\n        try:\n            import cStringIO as StringIO\n        except ImportError:\n            import StringIO\n\n        # check for a filename specified...if the user is passing binary data in\n        # video then a filename will be specified\n        if filename is None:\n            data = StringIO.StringIO(open(video, 'rb').read())\n        else:\n            # there was a filename specified, which indicates that video was not\n            # the path to an video file but rather the binary data of a file\n            data = StringIO.StringIO(video)\n            video = filename\n\n        content_type, body = self.__encode_multipart_formdata(\n            list(args.iteritems()), [(video, data)])\n\n        # Set the correct End Point Url, note this is different to all other FB\n        # EndPoints\n        urlinfo = urlparse.urlsplit(FACEBOOK_VIDEO_URL)\n        try:\n            content_length = len(body)\n            chunk_size = 4096\n\n            h = httplib.HTTPConnection(urlinfo[1])\n            h.putrequest('POST', urlinfo[2])\n            h.putheader('Content-Type', content_type)\n            h.putheader('Content-Length', str(content_length))\n            h.putheader('MIME-Version', '1.0')\n            h.putheader('User-Agent', 'PyFacebook Client Library')\n            h.endheaders()\n\n            if callback:\n                count = 0\n                while len(body) > 0:\n                    if len(body) < chunk_size:\n                        data = body\n                        body = ''\n                    else:\n                        data = body[0:chunk_size]\n                        body = body[chunk_size:]\n\n                    h.send(data)\n                    count += 1\n                    callback(count, chunk_size, content_length)\n            else:\n                h.send(body)\n\n            response = h.getresponse()\n\n            if response.status != 200:\n                raise Exception(\n                    'Error uploading video: Facebook returned HTTP %s (%s)' %\n                    (response.status, response.reason))\n            response = response.read()\n        except:\n            # sending the photo failed, perhaps we are using GAE\n            try:\n                from google.appengine.api import urlfetch\n\n                try:\n                    response = urlread(\n                        url=FACEBOOK_VIDEO_URL,\n                        data=body,\n                        headers={\n                            'POST': urlinfo[2],\n                            'Content-Type': content_type,\n                            'MIME-Version': '1.0'})\n                except urllib2.URLError:\n                    raise Exception(\n                        'Error uploading video: Facebook returned %s' %\n                        (response))\n            except ImportError:\n                # could not import from google.appengine.api, so we are not\n                # running in GAE\n                raise Exception('Error uploading video.')\n\n        return self._client._parse_response(response, 'facebook.video.upload')\n\n    def __encode_multipart_formdata(self, fields, files):\n        \"\"\"Encodes a multipart/form-data message to upload an image.\"\"\"\n        boundary = '-------tHISiStheMulTIFoRMbOUNDaRY'\n        crlf = '\\r\\n'\n        l = []\n\n        for (key, value) in fields:\n            l.append('--' + boundary)\n            l.append('Content-Disposition: form-data; name=\"%s\"' % str(key))\n            l.append('')\n            l.append(str(value))\n        for (filename, value) in files:\n            l.append('--' + boundary)\n            l.append(\n                'Content-Disposition: form-data; filename=\"%s\"' %\n                (str(filename), ))\n            l.append('Content-Type: %s' % self.__get_content_type(filename))\n            l.append('')\n            l.append(value.getvalue())\n        l.append('--' + boundary + '--')\n        l.append('')\n        body = crlf.join(l)\n        content_type = 'multipart/form-data; boundary=%s' % boundary\n        return content_type, body\n\n    def __get_content_type(self, filename):\n        \"\"\"Returns a guess at the MIME type of the file from the filename.\"\"\"\n        return str(mimetypes.guess_type(filename)[\n                   0]) or 'application/octet-stream'\n\n\nclass Facebook(object):\n    \"\"\"\n    Provides access to the Facebook API.\n\n    Instance Variables:\n\n    added\n        True if the user has added this application.\n\n    api_key\n        Your API key, as set in the constructor.\n\n    app_id\n        Your application id, as set in the constructor or fetched from\n        fb_sig_app_id request parameter.\n\n    app_name\n        Your application's name, i.e. the APP_NAME in http://apps.facebook.com/APP_NAME/ if\n        this is for an internal web application. Optional, but useful for automatic redirects\n        to canvas pages.\n\n    auth_token\n        The auth token that Facebook gives you, either with facebook.auth.createToken,\n        or through a GET parameter.\n\n    callback_path\n        The path of the callback set in the Facebook app settings. If your callback is set\n        to http://www.example.com/facebook/callback/, this should be '/facebook/callback/'.\n        Optional, but useful for automatic redirects back to the same page after login.\n\n    desktop\n        True if this is a desktop app, False otherwise. Used for determining how to\n        authenticate.\n\n    ext_perms\n        Any extended permissions that the user has granted to your application.\n        This parameter is set only if the user has granted any.\n\n    facebook_url\n        The url to use for Facebook requests.\n\n    facebook_secure_url\n        The url to use for secure Facebook requests.\n\n    in_canvas\n        True if the current request is for a canvas page.\n\n    in_iframe\n        True if the current request is for an HTML page to embed in Facebook inside an iframe.\n\n    is_session_from_cookie\n        True if the current request session comes from a session cookie.\n\n    in_profile_tab\n        True if the current request is for a user's tab for your application.\n\n    internal\n        True if this Facebook object is for an internal application (one that can be added on Facebook)\n\n    locale\n        The user's locale. Default: 'en_US'\n\n    oauth2:\n        Whether to use the new OAuth 2.0 authentication mechanism.  Default: False\n\n    oauth2_token:\n        The current OAuth 2.0 token.\n\n    oauth2_token_expires:\n        The UNIX time when the OAuth 2.0 token expires (seconds).\n\n    page_id\n        Set to the page_id of the current page (if any)\n\n    profile_update_time\n        The time when this user's profile was last updated. This is a UNIX timestamp. Default: None if unknown.\n\n    secret\n        Secret that is used after getSession for desktop apps.\n\n    secret_key\n        Your application's secret key, as set in the constructor.\n\n    session_key\n        The current session key. Set automatically by auth.getSession, but can be set\n        manually for doing infinite sessions.\n\n    session_key_expires\n        The UNIX time of when this session key expires, or 0 if it never expires.\n\n    uid\n        After a session is created, you can get the user's UID with this variable. Set\n        automatically by auth.getSession.\n\n    ----------------------------------------------------------------------\n\n    \"\"\"\n\n    def __init__(self, api_key, secret_key, auth_token=None, app_name=None,\n                 callback_path=None, internal=None, proxy=None,\n                 facebook_url=None, facebook_secure_url=None,\n                 generate_session_secret=0, app_id=None, oauth2=False,\n                 iframe_validation_timeout=10):\n        \"\"\"\n        Initializes a new Facebook object which provides wrappers for the Facebook API.\n\n        If this is a desktop application, the next couple of steps you might want to take are:\n\n        facebook.auth.createToken() # create an auth token\n        facebook.login()            # show a browser window\n        wait_login()                # somehow wait for the user to log in\n        facebook.auth.getSession()  # get a session key\n\n        For web apps, if you are passed an auth_token from Facebook, pass that in as a named parameter.\n        Then call:\n\n        facebook.auth.getSession()\n\n        \"\"\"\n        self.api_key = api_key\n        self.secret_key = secret_key\n        self.app_id = app_id\n        self.oauth2 = oauth2\n        self.oauth2_token = None\n        self.oauth2_token_expires = None\n        self.session_key = None\n        self.session_key_expires = None\n        self.auth_token = auth_token\n        self.secret = None\n        self.generate_session_secret = generate_session_secret\n        self.uid = None\n        self.page_id = None\n        self.in_canvas = False\n        self.in_iframe = False\n        self.is_session_from_cookie = False\n        self.in_profile_tab = False\n        self.added = False\n        self.app_name = app_name\n        self.callback_path = callback_path\n        self.internal = internal\n        self._friends = None\n        self.locale = 'en_US'\n        self.profile_update_time = None\n        self.ext_perms = []\n        self.proxy = proxy\n        self.iframe_validation_timeout = iframe_validation_timeout\n        if facebook_url is None:\n            self.facebook_url = FACEBOOK_URL\n        else:\n            self.facebook_url = facebook_url\n        if facebook_secure_url is None:\n            self.facebook_secure_url = FACEBOOK_SECURE_URL\n        else:\n            self.facebook_secure_url = facebook_secure_url\n\n        for namespace in METHODS:\n            self.__dict__[namespace] = eval(\n                '%sProxy(self, \\'%s\\')' %\n                (namespace.title(),\n                 'facebook.%s' %\n                 namespace))\n\n    def _hash_args(self, args, secret=None):\n        \"\"\"Hashes arguments by joining key=value pairs, appending a secret, and then taking the MD5 hex digest.\"\"\"\n        # @author: houyr\n        # fix for UnicodeEncodeError\n        hasher = hashlib.md5(\n            ''.join(\n                [\n                    '%s=%s' %\n                    (isinstance(\n                        x,\n                        unicode) and x.encode(\"utf-8\") or x,\n                        isinstance(\n                        args[x],\n                        unicode) and args[x].encode(\"utf-8\") or args[x]) for x in sorted(\n                        args.keys())]))\n        if secret:\n            hasher.update(secret)\n        elif self.secret:\n            hasher.update(self.secret)\n        else:\n            hasher.update(self.secret_key)\n        return hasher.hexdigest()\n\n    def _parse_response_item(self, node):\n        \"\"\"Parses an XML response node from Facebook.\"\"\"\n        if node.nodeType == node.DOCUMENT_NODE and \\\n                node.childNodes[0].hasAttributes() and \\\n                node.childNodes[0].hasAttribute('list') and \\\n                node.childNodes[0].getAttribute('list') == \"true\":\n            return {\n                node.childNodes[0].nodeName: self._parse_response_list(\n                    node.childNodes[0])}\n        elif node.nodeType == node.ELEMENT_NODE and \\\n                node.hasAttributes() and \\\n                node.hasAttribute('list') and \\\n                node.getAttribute('list') == \"true\":\n            return self._parse_response_list(node)\n        elif len(filter(lambda x: x.nodeType == x.ELEMENT_NODE, node.childNodes)) > 0:\n            return self._parse_response_dict(node)\n        else:\n            return ''.join(\n                node.data for node in node.childNodes if node.nodeType == node.TEXT_NODE)\n\n    def _parse_response_dict(self, node):\n        \"\"\"Parses an XML dictionary response node from Facebook.\"\"\"\n        result = {}\n        for item in filter(\n                lambda x: x.nodeType == x.ELEMENT_NODE,\n                node.childNodes):\n            result[item.nodeName] = self._parse_response_item(item)\n        if node.nodeType == node.ELEMENT_NODE and node.hasAttributes():\n            if node.hasAttribute('id'):\n                result['id'] = node.getAttribute('id')\n        return result\n\n    def _parse_response_list(self, node):\n        \"\"\"Parses an XML list response node from Facebook.\"\"\"\n        result = []\n        for item in filter(\n                lambda x: x.nodeType == x.ELEMENT_NODE,\n                node.childNodes):\n            result.append(self._parse_response_item(item))\n        return result\n\n    def _check_error(self, response):\n        \"\"\"Checks if the given Facebook response is an error, and then raises the appropriate exception.\"\"\"\n        if isinstance(response, dict) and 'error_code' in response:\n            raise FacebookError(\n                response['error_code'],\n                response['error_msg'],\n                response['request_args'])\n\n    def _build_post_args(self, method, args=None, signed=False):\n        \"\"\"Adds to args parameters that are necessary for every call to the API.\"\"\"\n        if args is None:\n            args = {}\n\n        for arg in args.items():\n            if isinstance(arg[1], list):\n                args[arg[0]] = ','.join(str(a) for a in arg[1])\n            elif isinstance(arg[1], unicode):\n                args[arg[0]] = arg[1].encode(\"UTF-8\")\n            elif isinstance(arg[1], bool):\n                args[arg[0]] = str(arg[1]).lower()\n\n        args['method'] = method\n        args['format'] = RESPONSE_FORMAT\n        if not signed and self.oauth2 and self.oauth2_token:\n            args['access_token'] = self.oauth2_token\n        else:\n            args['api_key'] = self.api_key\n            args['v'] = '1.0'\n            args['sig'] = self._hash_args(args)\n\n        return args\n\n    def _add_session_args(self, args=None):\n        \"\"\"Adds 'session_key' and 'call_id' to args, which are used for API calls that need sessions.\"\"\"\n        if args is None:\n            args = {}\n\n        if not self.session_key:\n            return args\n            # some calls don't need a session anymore. this might be better done in the markup\n            # raise RuntimeError('Session key not set. Make sure auth.getSession has been called.')\n\n        if not self.oauth2 or not self.oauth2_token:\n            args['session_key'] = self.session_key\n        args['call_id'] = str(int(time.time() * 1000))\n\n        return args\n\n    def _parse_response(self, response, method, format=None):\n        \"\"\"Parses the response according to the given (optional) format, which should be either 'JSON' or 'XML'.\"\"\"\n        if not format:\n            format = RESPONSE_FORMAT\n\n        if format == 'JSON':\n            result = simplejson.loads(response)\n\n            self._check_error(result)\n        elif format == 'XML':\n            dom = minidom.parseString(response)\n            result = self._parse_response_item(dom)\n            dom.unlink()\n\n            if 'error_response' in result:\n                self._check_error(result['error_response'])\n\n            result = result[method[9:].replace('.', '_') + '_response']\n        else:\n            raise RuntimeError('Invalid format specified.')\n\n        return result\n\n    def hash_email(self, email):\n        \"\"\"\n        Hash an email address in a format suitable for Facebook Connect.\n\n        \"\"\"\n        email = email.lower().strip()\n        return \"%s_%s\" % (\n            struct.unpack(\"I\", struct.pack(\"i\", binascii.crc32(email)))[0],\n            hashlib.md5(email).hexdigest(),\n        )\n\n    def unicode_urlencode(self, params):\n        \"\"\"\n        @author: houyr\n        A unicode aware version of urllib.urlencode.\n        \"\"\"\n        if isinstance(params, dict):\n            params = params.items()\n        return urllib.urlencode([(k, isinstance(v, unicode) and v.encode('utf-8') or v)\n                                 for k, v in params])\n\n    def __call__(self, method=None, args=None, secure=False, signed=False):\n        \"\"\"Make a call to Facebook's REST server.\"\"\"\n        # for Django templates, if this object is called without any arguments\n        # return the object itself\n        if method is None:\n            return self\n\n        # __init__ hard-codes into en_US\n        if args is not None and 'locale' not in args:\n            args['locale'] = self.locale\n\n        # @author: houyr\n        # fix for bug of UnicodeEncodeError\n        post_data = self.unicode_urlencode(\n            self._build_post_args(method, args, signed))\n\n        if self.proxy:\n            proxy_handler = urllib2.ProxyHandler(self.proxy)\n            opener = urllib2.build_opener(proxy_handler)\n            if self.oauth2 or secure:\n                response = opener.open(\n                    self.facebook_secure_url, post_data).read()\n            else:\n                response = opener.open(self.facebook_url, post_data).read()\n        else:\n            if self.oauth2 or secure:\n                response = urlread(self.facebook_secure_url, post_data)\n            else:\n                response = urlread(self.facebook_url, post_data)\n\n        return self._parse_response(response, method)\n\n    def oauth2_access_token(self, code, next, required_permissions=None):\n        \"\"\"\n        We've called authorize, and received a code, now we need to convert\n        this to an access_token\n\n        \"\"\"\n        args = {\n            'client_id': self.app_id,\n            'client_secret': self.secret_key,\n            'redirect_uri': next,\n            'code': code\n        }\n\n        if required_permissions:\n            args['scope'] = \",\".join(required_permissions)\n\n        # TODO see if immediate param works as per OAuth 2.0 spec?\n        url = self.get_graph_url('oauth/access_token', **args)\n\n        if self.proxy:\n            proxy_handler = urllib2.ProxyHandler(self.proxy)\n            opener = urllib2.build_opener(proxy_handler)\n            response = opener.open(url).read()\n        else:\n            response = urlread(url)\n\n        result = urlparse.parse_qs(response)\n        self.oauth2_token = result['access_token'][0]\n        self.oauth2_token_expires = time.time() + int(result['expires'][0])\n\n    # URL helpers\n    def get_url(self, page, **args):\n        \"\"\"\n        Returns one of the Facebook URLs (www.facebook.com/SOMEPAGE.php).\n        Named arguments are passed as GET query string parameters.\n\n        \"\"\"\n        return 'http://www.facebook.com/%s.php?%s' % (\n            page, urllib.urlencode(args))\n\n    def get_app_url(self, path=''):\n        \"\"\"\n        Returns the URL for this app's canvas page, according to app_name.\n\n        \"\"\"\n        return 'http://apps.facebook.com/%s/%s' % (self.app_name, path)\n\n    def get_graph_url(self, path='', **args):\n        \"\"\"\n        Returns the URL for the graph API with the supplied path and parameters\n\n        \"\"\"\n        return 'https://graph.facebook.com/%s?%s' % (\n            path, urllib.urlencode(args))\n\n    def get_add_url(self, next=None):\n        \"\"\"\n        Returns the URL that the user should be redirected to in order to add the application.\n\n        \"\"\"\n        args = {'api_key': self.api_key, 'v': '1.0'}\n\n        if next is not None:\n            args['next'] = next\n\n        return self.get_url('install', **args)\n\n    def get_authorize_url(self, next=None, next_cancel=None):\n        \"\"\"\n        Returns the URL that the user should be redirected to in order to\n        authorize certain actions for application.\n\n        \"\"\"\n        args = {'api_key': self.api_key, 'v': '1.0'}\n\n        if next is not None:\n            args['next'] = next\n\n        if next_cancel is not None:\n            args['next_cancel'] = next_cancel\n\n        return self.get_url('authorize', **args)\n\n    def get_login_url(self, next=None, popup=False, canvas=True,\n                      required_permissions=None):\n        \"\"\"\n        Returns the URL that the user should be redirected to in order to login.\n\n        next -- the URL that Facebook should redirect to after login\n        required_permissions -- permission required by the application\n\n        \"\"\"\n        if self.oauth2:\n            args = {\n                'client_id': self.app_id,\n                'redirect_uri': next\n            }\n\n            if required_permissions:\n                args['scope'] = required_permissions\n\n            if popup:\n                args['display'] = 'popup'\n\n            return self.get_graph_url('oauth/authorize', **args)\n        else:\n            args = {'api_key': self.api_key, 'v': '1.0'}\n\n            if next is not None:\n                args['next'] = next\n\n            if canvas is True:\n                args['canvas'] = 1\n\n            if popup is True:\n                args['popup'] = 1\n\n            if required_permissions:\n                args['req_perms'] = \",\".join(required_permissions)\n\n            if self.auth_token is not None:\n                args['auth_token'] = self.auth_token\n\n            return self.get_url('login', **args)\n\n    def login(self, popup=False):\n        \"\"\"Open a web browser telling the user to login to Facebook.\"\"\"\n        import webbrowser\n        webbrowser.open(self.get_login_url(popup=popup))\n\n    def get_ext_perm_url(self, ext_perm, next=None, popup=False):\n        \"\"\"\n        Returns the URL that the user should be redirected to in order to grant an extended permission.\n\n        ext_perm -- the name of the extended permission to request\n        next     -- the URL that Facebook should redirect to after login\n\n        \"\"\"\n        args = {'ext_perm': ext_perm, 'api_key': self.api_key, 'v': '1.0'}\n\n        if next is not None:\n            args['next'] = next\n\n        if popup is True:\n            args['popup'] = 1\n\n        return self.get_url('authorize', **args)\n\n    def request_extended_permission(self, ext_perm, popup=False):\n        \"\"\"Open a web browser telling the user to grant an extended permission.\"\"\"\n        import webbrowser\n        webbrowser.open(self.get_ext_perm_url(ext_perm, popup=popup))\n\n    def check_session(self, request, params=None):\n        \"\"\"\n        Checks the given Django HttpRequest for Facebook parameters such as\n        POST variables or an auth token. If the session is valid, returns True\n        and this object can now be used to access the Facebook API. Otherwise,\n        it returns False, and the application should take the appropriate action\n        (either log the user in or have him add the application).\n\n        \"\"\"\n        self.in_canvas = (request.POST.get('fb_sig_in_canvas') == '1')\n\n        if 'auth_token' not in request.GET and self.session_key and (\n                self.uid or self.page_id):\n            return True\n\n        if not params:\n            if request.method == 'POST':\n                params = self.validate_signature(request.POST)\n            else:\n                if 'installed' in request.GET:\n                    self.added = True\n\n                if 'fb_page_id' in request.GET:\n                    self.page_id = request.GET['fb_page_id']\n\n                if 'auth_token' in request.GET:\n                    self.added = True  # added by Marinho\n                    self.auth_token = request.GET['auth_token']\n\n                    try:\n                        self.auth.getSession()\n                    except FacebookError as e:\n                        self.auth_token = None\n                        return False\n\n                    return True\n\n                params = self.validate_signature(request.GET)\n\n        if not params:\n            cookies = None\n\n            # first check if we are in django - to check cookies\n            if hasattr(request, 'COOKIES'):\n                cookies = request.COOKIES\n            else:\n                if hasattr(request, 'cookies'):\n                    cookies = request.cookies\n\n            if cookies:\n\n                params = self.validate_oauth_cookie_signature(cookies)\n\n                if params:\n                    self.is_oauth = True\n                    self.oauth_token = params['access_token']\n                else:\n                    params = self.validate_cookie_signature(cookies)\n                    self.is_session_from_cookie = True\n\n        if not params:\n            if self.validate_iframe(request):\n                return True\n            else:\n                params = {}\n\n        if not params:\n            return False\n\n        if 'in_canvas' in params and params.get('in_canvas') == '1':\n            self.in_canvas = True\n\n        if 'in_iframe' in params and params.get('in_iframe') == '1':\n            self.in_iframe = True\n\n        if 'in_profile_tab' in params and params.get('in_profile_tab') == '1':\n            self.in_profile_tab = True\n\n        if 'added' in params and params.get('added') == '1':\n            self.added = True\n\n        if params.get('expires'):\n            # Marinho Brandao - fixing problem with no session\n            self.session_key_expires = params.get(\n                'expires', '').isdigit() and int(\n                params['expires']) or 0\n\n        if 'locale' in params:\n            self.locale = params['locale']\n\n        if 'profile_update_time' in params:\n            try:\n                self.profile_update_time = int(params['profile_update_time'])\n            except ValueError:\n                pass\n\n        if 'ext_perms' in params:\n            if params['ext_perms']:\n                self.ext_perms = params['ext_perms'].split(',')\n            else:\n                self.ext_perms = []\n\n        if 'friends' in params:\n            if params['friends']:\n                self._friends = params['friends'].split(',')\n            else:\n                self._friends = []\n\n        # If app_id is not set explicitly, pick it up from the param\n        if not self.app_id and 'app_id' in params:\n            self.app_id = params['app_id']\n\n        if 'session_key' in params:\n            self.session_key = params['session_key']\n            if 'user' in params:\n                self.uid = params['user']\n            elif 'page_id' in params:\n                self.page_id = params['page_id']\n            elif 'uid' in params:\n                self.uid = params['uid']\n            else:\n                return False\n        elif 'profile_session_key' in params:\n            self.session_key = params['profile_session_key']\n            if 'profile_user' in params:\n                self.uid = params['profile_user']\n            else:\n                return False\n        elif 'canvas_user' in params:\n            self.uid = params['canvas_user']\n        elif 'uninstall' in params:\n            self.uid = params['user']\n        else:\n            return False\n\n        return True\n\n    def validate_oauth_session(self, session_json):\n        session = simplejson.loads(session_json)\n        sig = session.pop('sig')\n        hash = self._hash_args(session)\n        if hash == sig:\n            return session\n        return None\n\n    def validate_signature(self, post, prefix='fb_sig', timeout=None):\n        \"\"\"\n        Validate parameters passed to an internal Facebook app from Facebook.\n\n        \"\"\"\n        args = post.copy()\n\n        if prefix not in args:\n            return None\n\n        del args[prefix]\n\n        if timeout and '%s_time' % prefix in post and time.time(\n        ) - float(post['%s_time' % prefix]) > timeout:\n            return None\n\n        args = dict([(key[len(prefix + '_'):], value)\n                     for key, value in args.items() if key.startswith(prefix)])\n\n        hash = self._hash_args(args)\n\n        if hash == post[prefix]:\n            return args\n        else:\n            return None\n\n    def validate_iframe(self, request):\n        request_dict = request.POST if request.method == 'POST' else request.GET\n        if any(\n            key not in request_dict for key in [\n                'userid',\n                'reqtime',\n                'appsig']):\n            return False\n        request_time = request_dict['reqtime']\n        time_now = int(time.time())\n        if time_now - int(request_time) > self.iframe_validation_timeout:\n            return False\n        userid = int(request_dict['userid'])\n        self.uid = userid\n        app_sig = request_dict['appsig']\n        digest = create_hmac(\n            self.secret_key, \"%s%s\" %\n            (str(userid), str(request_time)))\n        return digest == app_sig\n\n    def validate_oauth_cookie_signature(self, cookies):\n        cookie_name = 'fbs_%s' % self.app_id\n        if cookie_name in cookies:\n            # value like\n            # \"access_token=104302089510310%7C2.HYYLow1Vlib0s_sJSAESjw__.3600.1275037200-100000214342553%7CtC1aolM22Lauj_dZhYnv_tF2CK4.&base_domain=yaycy.com&expires=1275037200&secret=FvIHkbAFwEy_0sueRk2ZYQ__&session_key=2.HYYoow1Vlib0s_sJSAESjw__.3600.1275037200-100000214342553&sig=7bb035a0411be7aa801964ae34416f28&uid=100000214342553\"\n            params = dict([part.split('=')\n                           for part in cookies[cookie_name].split('&')])\n            sig = params.pop('sig')\n            hash = self._hash_args(params)\n            if hash == sig:\n                return params\n        return None\n\n    def validate_cookie_signature(self, cookies):\n        \"\"\"\n        Validate parameters passed by cookies, namely facebookconnect or js api.\n        \"\"\"\n\n        api_key = self.api_key\n        if api_key not in cookies:\n            return None\n\n        prefix = api_key + \"_\"\n\n        params = {}\n        vals = ''\n        for k in sorted(cookies):\n            if k.startswith(prefix):\n                key = k.replace(prefix, \"\")\n                value = cookies[k]\n                if value != 'None':\n                    params[key] = value\n                    vals += '%s=%s' % (key, value)\n\n        hasher = hashlib.md5(vals)\n\n        hasher.update(self.secret_key)\n        digest = hasher.hexdigest()\n        if digest == cookies[api_key]:\n            params['is_session_from_cookie'] = True\n            return params\n        else:\n            return False\n\n\nif __name__ == '__main__':\n    # sample desktop application\n\n    api_key = ''\n    secret_key = ''\n\n    facebook = Facebook(api_key, secret_key)\n\n    facebook.auth.createToken()\n\n    # Show login window\n    # Set popup=True if you want login without navigational elements\n    facebook.login()\n\n    # Login to the window, then press enter\n    print 'After logging in, press enter...'\n    raw_input()\n\n    facebook.auth.getSession()\n    print 'Session Key:   ', facebook.session_key\n    print 'Your UID:      ', facebook.uid\n\n    info = facebook.users.getInfo(\n        [facebook.uid], ['name', 'birthday', 'affiliations', 'sex'])[0]\n\n    print 'Your Name:     ', info['name']\n    print 'Your Birthday: ', info['birthday']\n    print 'Your Gender:   ', info['sex']\n\n    friends = facebook.friends.get()\n    friends = facebook.users.getInfo(\n        friends[\n            0:5], [\n            'name', 'birthday', 'relationship_status'])\n\n    for friend in friends:\n        print friend['name'], 'has a birthday on', friend['birthday'], 'and is', friend['relationship_status']\n\n    arefriends = facebook.friends.areFriends(\n        [friends[0]['uid']], [friends[1]['uid']])\n\n    photos = facebook.photos.getAlbums(facebook.uid)\n"
  },
  {
    "path": "facebook/djangofb/__init__.py",
    "content": "import re\nimport time\nimport datetime\nimport facebook\n\nfrom django.http import HttpResponse, HttpResponseRedirect\nfrom django.utils.http import urlquote\nfrom django.core.exceptions import ImproperlyConfigured\nfrom django.conf import settings\n\ntry:\n    from threading import local\nexcept ImportError:\n    from django.utils._threading_local import local\n\n__all__ = [\n    'Facebook',\n    'FacebookMiddleware',\n    'get_facebook_client',\n    'require_login',\n    'require_add']\n\n_thread_locals = local()\n\n\nclass Facebook(facebook.Facebook):\n\n    def redirect(self, url):\n        \"\"\"\n        Helper for Django which redirects to another page. If inside a\n        canvas page, writes a <fb:redirect> instead to achieve the same effect.\n\n        \"\"\"\n        if self.in_canvas:\n            return HttpResponse('<fb:redirect url=\"%s\" />' % (url,))\n        elif re.search(\"^https?:\\/\\/([^\\/]*\\.)?facebook\\.com(:\\d+)?\", url.lower()):\n            return HttpResponse(\n                '<script type=\"text/javascript\">\\ntop.location.href = \"%s\";\\n</script>' %\n                url)\n        else:\n            return HttpResponseRedirect(url)\n\n    def url_for(self, path):\n        \"\"\"\n        Expand the path into a full URL, depending on whether we're in a canvas\n        page or not.\n\n        \"\"\"\n        if self.in_canvas:\n            return self.get_app_url(path[1:])\n        else:\n            return '%s%s' % (settings.SITE_URL, path)\n\n    def _oauth2_process_params(self, request):\n        \"\"\"\n        Check a few key parameters for oauth methods\n\n        \"\"\"\n        self.in_canvas = (request.REQUEST.get('fb_sig_in_canvas') == '1')\n        self.added = (request.REQUEST.get('fb_sig_added') == '1')\n        # If app_id is not set explicitly, pick it up from the params\n        if not self.app_id:\n            self.app_id = request.REQUEST.get('fb_sig_app_id')\n        if not self.uid:\n            self.uid = request.REQUEST.get('fb_sig_user')\n\n    def oauth2_check_session(self, request):\n        \"\"\"\n        Check to see if we have an access_token in our session\n\n        \"\"\"\n        valid_token = False\n\n        # See if they're in the request\n        if 'session' in request.POST:\n            print 'session from POST'\n            values = self.validate_oauth_session(request.POST['session'])\n\n        # Might be in the query string (e.g. from iframe)\n        elif 'session' in request.GET:\n            print 'session from GET'\n            values = self.validate_oauth_session(request.GET['session'])\n\n        # Look out for an access_token in our cookies from the JS SDK FB.init\n        elif request.COOKIES:\n            values = self.validate_oauth_cookie_signature(request.COOKIES)\n            print 'session from COOKIE %s' % values\n\n        if values and 'access_token' in values:\n            request.session['oauth2_token'] = values['access_token']\n            request.session['oauth2_token_expires'] = values['expires']\n            self.session_key = values['session_key']\n            self.uid = values['uid']\n            self.added = True\n\n        # If we've been accepted by the user\n        if self.added:\n\n            # See if we've got this user's access_token in our session\n            if 'oauth2_token' in request.session:\n                self.oauth2_token = request.session['oauth2_token']\n                self.oauth2_token_expires = request.session[\n                    'oauth2_token_expires']\n\n            if self.oauth2_token_expires:\n                if self.oauth2_token_expires > time.time():\n                    # Got a token, and it's valid\n                    valid_token = True\n                else:\n                    del request.session['oauth2_token']\n                    del request.session['oauth2_token_expires']\n\n        return valid_token\n\n    def oauth2_check_permissions(self, request, required_permissions,\n                                 additional_permissions=None,\n                                 fql_check=True, force_check=True):\n        \"\"\"\n        Check for specific extended_permissions.\n\n        If fql_check is True (default), oauth2_check_session() should be called\n        first to ensure the access_token is in place and valid to make query.\n\n        \"\"\"\n        has_permissions = False\n\n        req_perms = set(required_permissions.split(','))\n\n        if 'oauth2_extended_permissions' in request.session:\n            cached_perms = request.session['oauth2_extended_permissions']\n\n        # so now, fb_sig_ext_perms seems to contain the right perms (!)\n\n        if not force_check and cached_perms and req_perms.issubset(\n                cached_perms):\n            # Note that this has the potential to be out of date!\n            has_permissions = True\n        elif fql_check:\n            # TODO allow option to use preload FQL for this?\n            perms_query = required_permissions\n\n            # Note that we can query additional permissions that we\n            # don't require.  This can be useful for optional\n            # functionality (or simply for better caching)\n            if additional_permissions:\n                perms_query += ',' + additional_permissions\n\n            perms_results = self.fql.query(\n                'select %s from permissions where uid=%s' %\n                (perms_query, self.uid))[0]\n            actual_perms = set()\n            for permission, allowed in perms_results.items():\n                if allowed == 1:\n                    actual_perms.add(permission)\n            request.session['oauth2_extended_permissions'] = actual_perms\n            has_permissions = req_perms.issubset(actual_perms)\n\n        return has_permissions\n\n    def oauth2_process_code(self, request, redirect_uri):\n        \"\"\"\n        Convert the code into an access_token.\n\n        \"\"\"\n        if 'code' in request.GET:\n            # We've got a code from an authorisation, so convert it to a\n            # access_token\n\n            self.oauth2_access_token(request.GET['code'], next=redirect_uri)\n\n            request.session['oauth2_token'] = self.oauth2_token\n            request.session['oauth2_token_expires'] = self.oauth2_token_expires\n\n            return True\n        # else: 'error_reason' in request.GET\n\n        return False\n\n\ndef get_facebook_client():\n    \"\"\"\n    Get the current Facebook object for the calling thread.\n\n    \"\"\"\n    try:\n        return _thread_locals.facebook\n    except AttributeError:\n        raise ImproperlyConfigured(\n            'Make sure you have the Facebook middleware installed.')\n\n\ndef _check_middleware(request):\n    try:\n        fb = request.facebook\n    except:\n        raise ImproperlyConfigured(\n            'Make sure you have the Facebook middleware installed.')\n\n    if not fb.oauth2:\n        raise ImproperlyConfigured(\n            'Please ensure that oauth2 is enabled (e.g. via settings.FACEBOOK_OAUTH2).')\n\n    return fb\n\n\ndef require_oauth(\n        redirect_path=None,\n        keep_state=True,\n        in_canvas=True,\n        required_permissions=None,\n        check_permissions=None,\n        force_check=True):\n    \"\"\"\n    Decorator for Django views that requires the user to be OAuth 2.0'd.\n    The FacebookMiddleware must be installed.\n    Note that OAuth 2.0 does away with the app added/logged in distinction -\n    it is now the case that users have now either authorised facebook users or\n    not, and if they are, they may have granted the app a number of\n    extended permissions - there is no lightweight/automatic login any more.\n\n    Standard usage:\n        @require_oauth()\n        def some_view(request):\n            ...\n    \"\"\"\n    def decorator(view):\n        def newview(request, *args, **kwargs):\n            # permissions=newview.permissions\n\n            try:\n                fb = _check_middleware(request)\n\n                valid_token = fb.oauth2_check_session(request)\n\n                if required_permissions:\n                    has_permissions = fb.oauth2_check_permissions(\n                        request, required_permissions, check_permissions,\n                        valid_token, force_check)\n                else:\n                    has_permissions = True\n\n                if not valid_token or not has_permissions:\n                    if in_canvas:\n                        fb.in_canvas = in_canvas\n\n                    return _redirect_login(request, fb, redirect_path,\n                                           keep_state, required_permissions)\n\n                return view(request, *args, **kwargs)\n            except facebook.FacebookError as e:\n                # Invalid token (I think this can happen if the user logs out)\n                # Unfortunately we don't find this out until we use the api\n                if e.code == 190:\n                    del request.session['oauth2_token']\n                    del request.session['oauth2_token_expires']\n                    return _redirect_login(request, fb, redirect_path,\n                                           keep_state, required_permissions)\n        # newview.permissions = permissions\n        return newview\n    return decorator\n\n\ndef _redirect_path(redirect_path, fb, path):\n    \"\"\"\n    Resolve the path to use for the redirect_uri for authorization\n\n    \"\"\"\n    if not redirect_path and fb.oauth2_redirect:\n        redirect_path = fb.oauth2_redirect\n    if redirect_path:\n        if callable(redirect_path):\n            redirect_path = redirect_path(path)\n    else:\n        redirect_path = path\n    return redirect_path\n\n\ndef _redirect_login(\n        request,\n        fb,\n        redirect_path,\n        keep_state,\n        required_permissions):\n    \"\"\"\n    Fully resolve the redirect path for an oauth login and add in any state\n    info required to bring us back to the correct place afterwards\n    \"\"\"\n    redirect_uri = fb.url_for(_redirect_path(redirect_path, fb, request.path))\n\n    if keep_state:\n        if callable(keep_state):\n            state = keep_state(request)\n        else:\n            state = request.get_full_path()\n        # passing state directly to facebook oauth endpoint doesn't work\n        redirect_uri += '?state=%s' % urlquote(state)\n\n    url = fb.get_login_url(next=redirect_uri,\n                           required_permissions=required_permissions)\n\n    return fb.redirect(url)\n\n\ndef process_oauth(restore_state=True, in_canvas=True):\n    \"\"\"\n    Decorator for Django views that processes the user's code and converts it\n    into an access_token.\n    The FacebookMiddleware must be installed.\n\n    Standard usage:\n        @process_oauth()\n        def some_view(request):\n            ...\n    \"\"\"\n    def decorator(view):\n        def newview(request, *args, **kwargs):\n            # permissions=newview.permissions\n\n            fb = _check_middleware(request)\n\n            if in_canvas:\n                fb.in_canvas = in_canvas\n\n            # Work out what the original redirect_uri value was\n            redirect_uri = fb.url_for(_strip_code(request.get_full_path()))\n\n            if fb.oauth2_process_code(request, redirect_uri):\n                if restore_state:\n                    state = request.GET['state']\n                    if callable(restore_state):\n                        state = restore_state(state)\n                    else:\n                        state = fb.url_for(state)\n                    return fb.redirect(state)\n\n            return view(request, *args, **kwargs)\n        # newview.permissions = permissions\n        return newview\n    return decorator\n\n\ndef _strip_code(path):\n    \"\"\"\n    Restore the path to the original redirect_uri without the code parameter.\n\n    \"\"\"\n    try:\n        begin = path.find('&code')\n        if begin == -1:\n            begin = path.index('?code')\n        end = path.find('&', begin + 1)\n        if end == -1:\n            end = len(path)\n        return path[:begin] + path[end:]\n    except ValueError:\n        # no code, probably failed to authenticate\n        # TODO strip error_reason instead here?\n        return path\n\n\ndef require_login(next=None, internal=None, required_permissions=None):\n    \"\"\"\n    Decorator for Django views that requires the user to be logged in.\n    The FacebookMiddleware must be installed.\n\n    Standard usage:\n        @require_login()\n        def some_view(request):\n            ...\n\n    Redirecting after login:\n        To use the 'next' parameter to redirect to a specific page after login, a callable should\n        return a path relative to the Post-add URL. 'next' can also be an integer specifying how many\n        parts of request.path to strip to find the relative URL of the canvas page. If 'next' is None,\n        settings.callback_path and settings.app_name are checked to redirect to the same page after logging\n        in. (This is the default behavior.)\n        @require_login(next=some_callable)\n        def some_view(request):\n            ...\n    \"\"\"\n    def decorator(view):\n        def newview(request, *args, **kwargs):\n            next = newview.next\n            internal = newview.internal\n\n            try:\n                fb = request.facebook\n            except:\n                raise ImproperlyConfigured(\n                    'Make sure you have the Facebook middleware installed.')\n\n            if internal is None:\n                internal = request.facebook.internal\n\n            if callable(next):\n                next = next(request.path)\n            elif isinstance(next, int):\n                next = '/'.join(request.path.split('/')[next + 1:])\n            elif next is None and fb.callback_path and request.path.startswith(fb.callback_path):\n                next = request.path[len(fb.callback_path):]\n            elif not isinstance(next, str):\n                next = ''\n\n            if internal and request.method == 'GET' and fb.app_name:\n                next = \"%s%s\" % (fb.get_app_url(), next)\n\n            try:\n                session_check = fb.check_session(request)\n            except ValueError:\n                session_check = False\n\n            if session_check and required_permissions:\n                req_perms = set(required_permissions)\n                perms = set(fb.ext_perms)\n                has_permissions = req_perms.issubset(perms)\n            else:\n                has_permissions = True\n\n            if not (session_check and has_permissions):\n                # If user has never logged in before, the get_login_url will\n                # redirect to the TOS page\n                return fb.redirect(\n                    fb.get_login_url(\n                        next=next,\n                        required_permissions=required_permissions))\n\n            return view(request, *args, **kwargs)\n        newview.next = next\n        newview.internal = internal\n        return newview\n    return decorator\n\n\ndef require_add(next=None, internal=None, on_install=None):\n    \"\"\"\n    Decorator for Django views that requires application installation.\n    The FacebookMiddleware must be installed.\n\n    Standard usage:\n        @require_add()\n        def some_view(request):\n            ...\n\n    Redirecting after installation:\n        To use the 'next' parameter to redirect to a specific page after login, a callable should\n        return a path relative to the Post-add URL. 'next' can also be an integer specifying how many\n        parts of request.path to strip to find the relative URL of the canvas page. If 'next' is None,\n        settings.callback_path and settings.app_name are checked to redirect to the same page after logging\n        in. (This is the default behavior.)\n        @require_add(next=some_callable)\n        def some_view(request):\n            ...\n\n    Post-install processing:\n        Set the on_install parameter to a callable in order to handle special post-install processing.\n        The callable should take a request object as the parameter.\n        @require_add(on_install=some_callable)\n        def some_view(request):\n            ...\n    \"\"\"\n    def decorator(view):\n        def newview(request, *args, **kwargs):\n            next = newview.next\n            internal = newview.internal\n\n            try:\n                fb = request.facebook\n            except:\n                raise ImproperlyConfigured(\n                    'Make sure you have the Facebook middleware installed.')\n\n            if internal is None:\n                internal = request.facebook.internal\n\n            if callable(next):\n                next = next(request.path)\n            elif isinstance(next, int):\n                next = '/'.join(request.path.split('/')[next + 1:])\n            elif next is None and fb.callback_path and request.path.startswith(fb.callback_path):\n                next = request.path[len(fb.callback_path):]\n            else:\n                next = ''\n\n            if not fb.check_session(request):\n                if fb.added:\n                    if request.method == 'GET' and fb.app_name:\n                        return fb.redirect('%s%s' % (fb.get_app_url(), next))\n                    return fb.redirect(fb.get_login_url(next=next))\n                else:\n                    return fb.redirect(fb.get_add_url(next=next))\n\n            if not fb.added:\n                return fb.redirect(fb.get_add_url(next=next))\n\n            if 'installed' in request.GET and callable(on_install):\n                on_install(request)\n\n            if internal and request.method == 'GET' and fb.app_name:\n                return fb.redirect('%s%s' % (fb.get_app_url(), next))\n\n            return view(request, *args, **kwargs)\n        newview.next = next\n        newview.internal = internal\n        return newview\n    return decorator\n\n# try to preserve the argspecs\ntry:\n    import decorator\nexcept ImportError:\n    pass\nelse:\n    # Can this be done with functools.wraps, but maintaining kwargs?\n    def updater(f):\n        def updated(*args, **kwargs):\n            original = f(*args, **kwargs)\n\n            def newdecorator(view):\n                return decorator.new_wrapper(original(view), view)\n            return decorator.new_wrapper(newdecorator, original)\n        return decorator.new_wrapper(updated, f)\n    require_oauth = updater(require_oauth)\n    process_oauth = updater(process_oauth)\n    require_login = updater(require_login)\n    require_add = updater(require_add)\n\n\nclass FacebookMiddleware(object):\n    \"\"\"\n    Middleware that attaches a Facebook object to every incoming request.\n    The Facebook object created can also be accessed from models for the\n    current thread by using get_facebook_client().\n\n    callback_path can be a string or a callable.  Using a callable lets us\n    pass in something like lambda reverse('our_canvas_view') so we can follow\n    the DRY principle.\n\n    \"\"\"\n\n    def __init__(self, api_key=None, secret_key=None, app_name=None,\n                 callback_path=None, internal=None, app_id=None,\n                 oauth2=None, oauth2_redirect=None):\n        self.api_key = api_key or settings.FACEBOOK_API_KEY\n        self.secret_key = secret_key or settings.FACEBOOK_SECRET_KEY\n        self.app_name = app_name or getattr(\n            settings, 'FACEBOOK_APP_NAME', None)\n        self.callback_path = callback_path or getattr(\n            settings, 'FACEBOOK_CALLBACK_PATH', None)\n        self.internal = internal or getattr(\n            settings, 'FACEBOOK_INTERNAL', True)\n        self.app_id = app_id or getattr(settings, 'FACEBOOK_APP_ID', None)\n        self.oauth2 = oauth2 or getattr(settings, 'FACEBOOK_OAUTH2', False)\n        self.oauth2_redirect = oauth2_redirect or getattr(\n            settings, 'FACEBOOK_OAUTH2_REDIRECT', None)\n        self.proxy = None\n        if getattr(settings, 'USE_HTTP_PROXY', False):\n            self.proxy = settings.HTTP_PROXY\n\n    def process_request(self, request):\n        callback_path = self.callback_path\n        if callable(callback_path):\n            callback_path = callback_path()\n        _thread_locals.facebook = request.facebook = Facebook(\n            self.api_key,\n            self.secret_key,\n            app_name=self.app_name,\n            callback_path=callback_path,\n            internal=self.internal,\n            proxy=self.proxy,\n            app_id=self.app_id,\n            oauth2=self.oauth2)\n        if self.oauth2:\n            if self.oauth2_redirect:\n                request.facebook.oauth2_redirect = self.oauth2_redirect\n            request.facebook._oauth2_process_params(request)\n        if not self.internal:\n            if 'fb_sig_session_key' in request.GET and (\n                    'fb_sig_user' in request.GET or 'fb_sig_canvas_user' in request.GET):\n                request.facebook.session_key = request.session[\n                    'facebook_session_key'] = request.GET['fb_sig_session_key']\n                request.facebook.uid = request.session['facebook_user_id'] = request.GET[\n                    'fb_sig_user'] or request.GET['fb_sig_canvas_user']\n            elif int(request.GET.get('fb_sig_added', '1')) and request.session.get('facebook_session_key', None) \\\n                    and request.session.get('facebook_user_id', None):\n                request.facebook.session_key = request.session[\n                    'facebook_session_key']\n                request.facebook.uid = request.session['facebook_user_id']\n\n    def process_response(self, request, response):\n\n        # Don't assume that request.facebook exists\n        # - it's not necessarily true that all process_requests will have been called\n        try:\n            fb = request.facebook\n        except AttributeError:\n            return response\n\n        if not self.internal and fb.session_key and fb.uid:\n            request.session['facebook_session_key'] = fb.session_key\n            request.session['facebook_user_id'] = fb.uid\n\n            if fb.session_key_expires:\n                expiry = datetime.datetime.utcfromtimestamp(\n                    fb.session_key_expires)\n                request.session.set_expiry(expiry)\n\n        if not fb.is_session_from_cookie:\n            # Make sure the browser accepts our session cookies inside an\n            # Iframe\n            response['P3P'] = 'CP=\"NOI DSP COR NID ADMa OPTa OUR NOR\"'\n            fb_cookies = {\n                'expires': fb.session_key_expires,\n                'session_key': fb.session_key,\n                'user': fb.uid,\n            }\n            fb_cookies = dict((k, v) for k, v in fb_cookies.items()\n                              if v is not None)\n\n            expire_time = None\n            if fb.session_key_expires:\n                expire_time = datetime.datetime.utcfromtimestamp(\n                    fb.session_key_expires)\n\n            for k in fb_cookies:\n                response.set_cookie(\n                    self.api_key + '_' + k,\n                    fb_cookies[k],\n                    expires=expire_time)\n            if fb_cookies:\n                response.set_cookie(\n                    self.api_key,\n                    fb._hash_args(fb_cookies),\n                    expires=expire_time)\n\n        return response\n"
  },
  {
    "path": "facebook/djangofb/context_processors.py",
    "content": "def messages(request):\n    \"\"\"Returns messages similar to ``django.core.context_processors.auth``.\"\"\"\n    if hasattr(request, 'facebook') and request.facebook.uid is not None:\n        from models import Message\n        messages = Message.objects.get_and_delete_all(uid=request.facebook.uid)\n    return {'messages': messages}\n"
  },
  {
    "path": "facebook/djangofb/default_app/__init__.py",
    "content": ""
  },
  {
    "path": "facebook/djangofb/default_app/models.py",
    "content": "from django.db import models\n\n# get_facebook_client lets us get the current Facebook object\n# from outside of a view, which lets us have cleaner code\nfrom facebook.djangofb import get_facebook_client\n\n\ndef _2int(d, k):\n    try:\n        d = d.__dict__\n    except:\n        pass\n\n    t = d.get(k, '')\n    if t == 'None':\n        t = 0\n    else:\n        t = int(t)\n    return t\n\n\nclass UserManager(models.Manager):\n    \"\"\"Custom manager for a Facebook User.\"\"\"\n\n    def get_current(self):\n        \"\"\"Gets a User object for the logged-in Facebook user.\"\"\"\n        facebook = get_facebook_client()\n        user, created = self.get_or_create(id=_2int(facebook, 'uid'))\n        if created:\n            # we could do some custom actions for new users here...\n            pass\n        return user\n\n\nclass User(models.Model):\n    \"\"\"A simple User model for Facebook users.\"\"\"\n\n    # We use the user's UID as the primary key in our database.\n    id = models.IntegerField(primary_key=True)\n\n    # TODO: The data that you want to store for each user would go here.\n    # For this sample, we let users let people know their favorite progamming\n    # language, in the spirit of Extended Info.\n    language = models.CharField(max_length=64, default='Python')\n\n    # Add the custom manager\n    objects = UserManager()\n"
  },
  {
    "path": "facebook/djangofb/default_app/templates/canvas.fbml",
    "content": "<fb:header>\r\n  {% comment %}\r\n    We can use {{ fbuser }} to get at the current user.\r\n    {{ fbuser.id }} will be the user's UID, and {{ fbuser.language }}\r\n    is his/her favorite language (Python :-).\r\n  {% endcomment %}\r\n  Welcome, <fb:name uid=\"{{ fbuser.id }}\" firstnameonly=\"true\" useyou=\"false\" />!\r\n</fb:header>\r\n\r\n<div class=\"clearfix\" style=\"float: left; border: 1px #d8dfea solid; padding: 10px 10px 10px 10px; margin-left: 30px; margin-bottom: 30px; width: 500px;\">\r\n  Your favorite language is {{ fbuser.language|escape }}.\r\n  <br /><br />\r\n\r\n  <div class=\"grayheader clearfix\">\r\n    <br /><br />\r\n\r\n    <form action=\".\" method=\"POST\">\r\n      <input type=\"text\" name=\"language\" value=\"{{ fbuser.language|escape }}\" />\r\n      <input type=\"submit\" value=\"Change\" />\r\n    </form>\r\n  </div>\r\n</div>\r\n"
  },
  {
    "path": "facebook/djangofb/default_app/urls.py",
    "content": "from django.conf.urls.defaults import *\n\nurlpatterns = patterns('{{ project }}.{{ app }}.views',\n                       (r'^$', 'canvas'),\n                       # Define other pages you want to create here\n                       )\n"
  },
  {
    "path": "facebook/djangofb/default_app/views.py",
    "content": "from django.http import HttpResponse\nfrom django.views.generic.simple import direct_to_template\n# uncomment the following two lines and the one below\n# if you dont want to use a decorator instead of the middleware\n# from django.utils.decorators import decorator_from_middleware\n# from facebook.djangofb import FacebookMiddleware\n\n# Import the Django helpers\nimport facebook.djangofb as facebook\n\n# The User model defined in models.py\nfrom models import User\n\n# We'll require login for our canvas page. This\n# isn't necessarily a good idea, as we might want\n# to let users see the page without granting our app\n# access to their info. See the wiki for details on how\n# to do this.\n# @decorator_from_middleware(FacebookMiddleware)\n\n\n@facebook.require_login()\ndef canvas(request):\n    # Get the User object for the currently logged in user\n    user = User.objects.get_current()\n\n    # Check if we were POSTed the user's new language of choice\n    if 'language' in request.POST:\n        user.language = request.POST['language'][:64]\n        user.save()\n\n    # User is guaranteed to be logged in, so pass canvas.fbml\n    # an extra 'fbuser' parameter that is the User object for\n    # the currently logged in user.\n    return direct_to_template(\n        request,\n        'canvas.fbml',\n        extra_context={\n            'fbuser': user})\n\n\n@facebook.require_login()\ndef ajax(request):\n    return HttpResponse('hello world')\n"
  },
  {
    "path": "facebook/djangofb/models.py",
    "content": "from django.db import models\r\nfrom django.utils.html import escape\r\nfrom django.utils.safestring import mark_safe\r\n\r\nFB_MESSAGE_STATUS = (\r\n    (0, 'Explanation'),\r\n    (1, 'Error'),\r\n    (2, 'Success'),\r\n)\r\n\r\n\r\nclass MessageManager(models.Manager):\r\n\r\n    def get_and_delete_all(self, uid):\r\n        messages = []\r\n        for m in self.filter(uid=uid):\r\n            messages.append(m)\r\n            m.delete()\r\n        return messages\r\n\r\n\r\nclass Message(models.Model):\r\n    \"\"\"Represents a message for a Facebook user.\"\"\"\r\n    uid = models.CharField(max_length=25)\r\n    status = models.IntegerField(choices=FB_MESSAGE_STATUS)\r\n    message = models.CharField(max_length=300)\r\n    objects = MessageManager()\r\n\r\n    def __unicode__(self):\r\n        return self.message\r\n\r\n    def _fb_tag(self):\r\n        return self.get_status_display().lower()\r\n\r\n    def as_fbml(self):\r\n        return mark_safe(u'<fb:%s message=\"%s\" />' % (\r\n            self._fb_tag(),\r\n            escape(self.message),\r\n        ))\r\n"
  },
  {
    "path": "facebook/webappfb.py",
    "content": "#\n# webappfb - Facebook tools for Google's AppEngine \"webapp\" Framework\n#\n# Copyright (c) 2009, Max Battcher\n# All rights reserved.\n#\n# Redistribution and use in source and binary forms, with or without\n# modification, are permitted provided that the following conditions are met:\n#     * Redistributions of source code must retain the above copyright\n#       notice, this list of conditions and the following disclaimer.\n#     * Redistributions in binary form must reproduce the above copyright\n#       notice, this list of conditions and the following disclaimer in the\n#       documentation and/or other materials provided with the distribution.\n#     * Neither the name of the author nor the names of its contributors may\n#       be used to endorse or promote products derived from this software\n#       without specific prior written permission.\n#\n# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS``AS IS'' AND ANY\n# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\n# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\n# DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY\n# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\n# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\n# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\n# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\n# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\nfrom google.appengine.api import memcache\nfrom google.appengine.ext.webapp import RequestHandler\nfrom facebook import Facebook\nimport yaml\n\n\"\"\"\nFacebook tools for Google AppEngine's object-oriented \"webapp\" framework.\n\"\"\"\n\n# This global configuration dictionary is for configuration variables\n# for Facebook requests such as the application's API key and secret\n# key. Defaults to loading a 'facebook.yaml' YAML file. This should be\n# useful and familiar for most AppEngine development.\nFACEBOOK_CONFIG = yaml.load(file('facebook.yaml', 'r'))\n\n\nclass FacebookRequestHandler(RequestHandler):\n    \"\"\"\n    Base class for request handlers for Facebook apps, providing useful\n    Facebook-related tools: a local\n    \"\"\"\n\n    def _fbconfig_value(self, name, default=None):\n        \"\"\"\n        Checks the global config dictionary and then for a class/instance\n        variable, using a provided default if no value is found.\n        \"\"\"\n        if name in FACEBOOK_CONFIG:\n            default = FACEBOOK_CONFIG[name]\n\n        return getattr(self, name, default)\n\n    def initialize(self, request, response):\n        \"\"\"\n        Initialize's this request's Facebook client.\n        \"\"\"\n        super(FacebookRequestHandler, self).initialize(request, response)\n\n        app_name = self._fbconfig_value('app_name', '')\n        api_key = self._fbconfig_value('api_key', None)\n        secret_key = self._fbconfig_value('secret_key', None)\n\n        self.facebook = Facebook(api_key, secret_key,\n                                 app_name=app_name)\n\n        require_app = self._fbconfig_value('require_app', False)\n        require_login = self._fbconfig_value('require_login', False)\n        need_session = self._fbconfig_value('need_session', False)\n        check_session = self._fbconfig_value('check_session', True)\n\n        self._messages = None\n        self.redirecting = False\n\n        if require_app or require_login:\n            if not self.facebook.check_session(request):\n                self.redirect(self.facebook.get_login_url(next=request.url))\n                self.redirecting = True\n                return\n        elif check_session:\n            self.facebook.check_session(request)  # ignore response\n\n        # NOTE: require_app is deprecated according to modern Facebook login\n        #       policies. Included for completeness, but unnecessary.\n        if require_app and not self.facebook.added:\n            self.redirect(self.facebook.get_add_url(next=request.url))\n            self.redirecting = True\n            return\n\n        if not (require_app or require_login) and need_session:\n            self.facebook.auth.getSession()\n\n    def redirect(self, url, **kwargs):\n        \"\"\"\n        For Facebook canvas pages we should use <fb:redirect /> instead of\n        a normal redirect.\n        \"\"\"\n        if self.facebook.in_canvas:\n            self.response.clear()\n            self.response.out.write('<fb:redirect url=\"%s\" />' % (url, ))\n        else:\n            super(FacebookRequestHandler, self).redirect(url, **kwargs)\n\n    def add_user_message(self, kind, msg, detail='', time=15 * 60):\n        \"\"\"\n        Add a message to the current user to memcache.\n        \"\"\"\n        if self.facebook.uid:\n            key = 'messages:%s' % self.facebook.uid\n            self._messages = memcache.get(key)\n            message = {\n                'kind': kind,\n                'message': msg,\n                'detail': detail,\n            }\n            if self._messages is not None:\n                self._messages.append(message)\n            else:\n                self._messages = [message]\n            memcache.set(key, self._messages, time=time)\n\n    def get_and_delete_user_messages(self):\n        \"\"\"\n        Get all of the messages for the current user; removing them.\n        \"\"\"\n        if self.facebook.uid:\n            key = 'messages:%s' % self.facebook.uid\n            if not hasattr(self, '_messages') or self._messages is None:\n                self._messages = memcache.get(key)\n            memcache.delete(key)\n            return self._messages\n        return None\n\n\nclass FacebookCanvasHandler(FacebookRequestHandler):\n    \"\"\"\n    Request handler for Facebook canvas (FBML application) requests.\n    \"\"\"\n\n    def canvas(self, *args, **kwargs):\n        \"\"\"\n        This will be your handler to deal with Canvas requests.\n        \"\"\"\n        raise NotImplementedError()\n\n    def get(self, *args):\n        \"\"\"\n        All valid canvas views are POSTS.\n        \"\"\"\n        # TODO: Attempt to auto-redirect to Facebook canvas?\n        self.error(404)\n\n    def post(self, *args, **kwargs):\n        \"\"\"\n        Check a couple of simple safety checks and then call the canvas\n        handler.\n        \"\"\"\n        if self.redirecting:\n            return\n\n        if not self.facebook.in_canvas:\n            self.error(404)\n            return\n\n        self.canvas(*args, **kwargs)\n\n# vim: ai et ts=4 sts=4 sw=4\n"
  },
  {
    "path": "facebook/wsgi.py",
    "content": "\"\"\"This is some simple helper code to bridge the Pylons / PyFacebook gap.\n\nThere's some generic WSGI middleware, some Paste stuff, and some Pylons\nstuff.  Once you put FacebookWSGIMiddleware into your middleware stack,\nyou'll have access to ``environ[\"pyfacebook.facebook\"]``, which is a\n``facebook.Facebook`` object.  If you're using Paste (which includes\nPylons users), you can also access this directly using the facebook\nglobal in this module.\n\n\"\"\"\n\n# Be careful what you import.  Don't expect everyone to have Pylons,\n# Paste, etc. installed.  Degrade gracefully.\n\nfrom facebook import Facebook\n\n__docformat__ = \"restructuredtext\"\n\n\n# Setup Paste, if available.  This needs to stay in the same module as\n# FacebookWSGIMiddleware below.\n\ntry:\n    from paste.registry import StackedObjectProxy\n    from webob.exc import _HTTPMove\n    try:\n        from string import Template\n    except ImportError:\n        from webob.util.stringtemplate import Template\n    from webob import html_escape\n\nexcept ImportError:\n    pass\nelse:\n    facebook = StackedObjectProxy(name=\"PyFacebook Facebook Connection\")\n\n    class CanvasRedirect(_HTTPMove):\n        \"\"\"This is for canvas redirects.\"\"\"\n\n        title = \"See Other\"\n        code = 200\n        html_template_obj = Template('<fb:redirect url=\"${location}\" />')\n\n        def html_body(self, environ):\n            return self.html_template_obj.substitute(location=self.detail)\n\n\nclass FacebookWSGIMiddleware(object):\n\n    \"\"\"This is WSGI middleware for Facebook.\"\"\"\n\n    def __init__(self, app, config, facebook_class=Facebook):\n        \"\"\"Initialize the Facebook middleware.\n\n        ``app``\n            This is the WSGI application being wrapped.\n\n        ``config``\n            This is a dict containing the keys \"pyfacebook.apikey\" and\n            \"pyfacebook.secret\".\n\n        ``facebook_class``\n            If you want to subclass the Facebook class, you can pass in\n            your replacement here.  Pylons users will want to use\n            PylonsFacebook.\n\n        \"\"\"\n        self.app = app\n        self.config = config\n        self.facebook_class = facebook_class\n\n    def __call__(self, environ, start_response):\n        config = self.config\n        real_facebook = self.facebook_class(config[\"pyfacebook.apikey\"],\n                                            config[\"pyfacebook.secret\"])\n        registry = environ.get('paste.registry')\n        if registry:\n            registry.register(facebook, real_facebook)\n        environ['pyfacebook.facebook'] = real_facebook\n        return self.app(environ, start_response)\n\n\n# The remainder is Pylons specific.\n\ntry:\n    import pylons\n    from pylons.controllers.util import redirect_to as pylons_redirect_to\n    from routes import url_for\nexcept ImportError:\n    pass\nelse:\n\n    class PylonsFacebook(Facebook):\n\n        \"\"\"Subclass Facebook to add Pylons goodies.\"\"\"\n\n        def check_session(self, request=None):\n            \"\"\"The request parameter is now optional.\"\"\"\n            if request is None:\n                request = pylons.request\n            return Facebook.check_session(self, request)\n\n        # The Django request object is similar enough to the Paste\n        # request object that check_session and validate_signature\n        # should *just work*.\n\n        def redirect_to(self, url):\n            \"\"\"Wrap Pylons' redirect_to function so that it works in_canvas.\n\n            By the way, this won't work until after you call\n            check_session().\n\n            \"\"\"\n            if self.in_canvas:\n                raise CanvasRedirect(url)\n            pylons_redirect_to(url)\n\n        def apps_url_for(self, *args, **kargs):\n            \"\"\"Like url_for, but starts with \"http://apps.facebook.com\".\"\"\"\n            return \"http://apps.facebook.com\" + url_for(*args, **kargs)\n\n    def create_pylons_facebook_middleware(app, config):\n        \"\"\"This is a simple wrapper for FacebookWSGIMiddleware.\n\n        It passes the correct facebook_class.\n\n        \"\"\"\n        return FacebookWSGIMiddleware(app, config,\n                                      facebook_class=PylonsFacebook)\n"
  },
  {
    "path": "setup.py",
    "content": "#!/usr/bin/env python\n\nfrom setuptools import setup, find_packages\n\nsetup(name='pyfacebook',\n      version='1.0a2.1',\n      description='Python Client Library for the Facebook API',\n      author='Samuel Cormier-Iijima',\n      author_email='sciyoshi@gmail.com',\n      url='http://code.google.com/p/pyfacebook',\n      packages=['facebook',\n                'facebook.djangofb',\n                'facebook.djangofb.default_app'],\n      test_suite='tests',\n      tests_require=['MiniMock'])\n"
  },
  {
    "path": "tests/__init__.py",
    "content": ""
  },
  {
    "path": "tests/test.py",
    "content": "import unittest\r\nimport sys\r\nimport os\r\nimport facebook\r\nimport urllib2\r\ntry:\r\n    from hashlib import md5\r\n    md5_constructor = md5\r\nexcept ImportError:\r\n    import md5\r\n    md5_constructor = md5.new\r\ntry:\r\n    import simplejson\r\nexcept ImportError:\r\n    from django.utils import simplejson\r\nimport httplib\r\nfrom minimock import Mock\r\n\r\nmy_api_key = \"e1e9cfeb5e0d7a52e4fbd5d09e1b873e\"\r\nmy_secret_key = \"1bebae7283f5b79aaf9b851addd55b90\"\r\n# '{\"error_code\":100,\\\r\n# \"error_msg\":\"Invalid parameter\",\\\r\n# \"request_args\":[{\"key\":\"format\",\"value\":\"JSON\"},\\\r\n# {\"key\":\"auth_token\",\"value\":\"24626e24bb12919f2f142145070542e8\"},\\\r\n# {\"key\":\"sig\",\"value\":\"36af2af3b93da784149301e77cb1621a\"},\\\r\n# {\"key\":\"v\",\"value\":\"1.0\"},\\\r\n# {\"key\":\"api_key\",\"value\":\"e1e9cfeb5e0d7a52e4fbd5d09e1b873e\"},\\\r\n# {\"key\":\"method\",\"value\":\"facebook.auth.getSession\"}]}'\r\nresponse_str = '{\"stuff\":\"abcd\"}'\r\n\r\n\r\nclass MyUrlOpen:\r\n\r\n    def __init__(self, *args, **kwargs):\r\n        pass\r\n\r\n    def read(self):\r\n        global response_str\r\n        return response_str\r\n\r\n\r\nclass pyfacebook_UnitTests(unittest.TestCase):\r\n\r\n    def setUp(self):\r\n        facebook.urllib2.urlopen = Mock('urllib2.urlopen')\r\n        facebook.urllib2.urlopen.mock_returns_func = MyUrlOpen\r\n        pass\r\n\r\n    def tearDown(self):\r\n        pass\r\n\r\n    def login(self):\r\n        pass\r\n\r\n    def test1(self):\r\n        f = facebook.Facebook(api_key=my_api_key, secret_key=my_secret_key)\r\n        f.login = self.login\r\n        self.assertEquals(f.api_key, my_api_key)\r\n        self.assertEquals(f.secret_key, my_secret_key)\r\n        self.assertEquals(f.auth_token, None)\r\n        self.assertEquals(f.app_name, None)\r\n        self.assertEquals(f.callback_path, None)\r\n        self.assertEquals(f.internal, None)\r\n\r\n    def test2(self):\r\n        args = {\"arg1\": \"a\", \"arg2\": \"b\", \"arg3\": \"c\"}\r\n        hasher = md5_constructor(\r\n            ''.join(['%s=%s' % (x, args[x]) for x in sorted(args.keys())]))\r\n        hasher.update(\"acdnj\")\r\n        f = facebook.Facebook(api_key=\"abcdf\", secret_key=\"acdnj\")\r\n        f.login = self.login\r\n        digest = f._hash_args(args)\r\n        self.assertEquals(hasher.hexdigest(), digest)\r\n        hasher = md5_constructor(\r\n            ''.join(['%s=%s' % (x, args[x]) for x in sorted(args.keys())]))\r\n        hasher.update(\"klmn\")\r\n        # trunk code has error hash.updated instead of hash.update\r\n        digest = f._hash_args(args, secret=\"klmn\")\r\n        self.assertEquals(hasher.hexdigest(), digest)\r\n\r\n        hasher = md5_constructor(\r\n            ''.join(['%s=%s' % (x, args[x]) for x in sorted(args.keys())]))\r\n        f.secret = \"klmn\"\r\n        hasher.update(f.secret)\r\n        # trunk code has error hash.updated instead of hash.update\r\n        digest = f._hash_args(args)\r\n        self.assertEquals(hasher.hexdigest(), digest)\r\n\r\n    def test3(self):\r\n        global response_str\r\n        response = {'stuff': 'abcd'}\r\n        response_str = simplejson.dumps(response)\r\n        fb = facebook.Facebook(my_api_key, my_secret_key)\r\n        fb.login = self.login\r\n        fb.auth.createToken()\r\n        self.assertEquals(str(fb.auth_token['stuff']), \"abcd\")\r\n        fb.login()\r\n        response = {\r\n            \"session_key\": \"key\",\r\n            \"uid\": \"my_uid\",\r\n            \"secret\": \"my_secret\",\r\n            \"expires\": \"my_expires\"}\r\n        response_str = simplejson.dumps(response)\r\n        res = fb.auth.getSession()\r\n        self.assertEquals(str(res[\"expires\"]), response[\"expires\"])\r\n        self.assertEquals(str(res[\"secret\"]), response[\"secret\"])\r\n        self.assertEquals(str(res[\"session_key\"]), response[\"session_key\"])\r\n        self.assertEquals(str(res[\"uid\"]), response[\"uid\"])\r\n\r\n    def test4(self):\r\n        global response_str\r\n        response = 'abcdef'\r\n        response_str = simplejson.dumps(response)\r\n        fb = facebook.Facebook(my_api_key, my_secret_key)\r\n        fb.login = self.login\r\n        fb.auth.createToken()\r\n        self.assertEquals(str(fb.auth_token), \"abcdef\")\r\n        url = fb.get_login_url(next=\"nowhere\", popup=True, canvas=True)\r\n        self.assertEquals(\r\n            url,\r\n            'http://www.facebook.com/login.php?canvas=1&popup=1&auth_token=abcdef&next=nowhere&v=1.0&api_key=%s' %\r\n            (my_api_key,\r\n             ))\r\n\r\n    def test5(self):\r\n        class Request:\r\n\r\n            def __init__(self, post, get, method):\r\n                self.POST = post\r\n                self.GET = get\r\n                self.method = method\r\n\r\n        req = Request({'fb_sig_in_canvas': 1}, {}, 'POST')\r\n        fb = facebook.Facebook(my_api_key, my_secret_key)\r\n        fb.login = self.login\r\n        res = fb.check_session(req)\r\n        self.assertFalse(res)\r\n        req = Request({'fb_sig': 1}, {}, 'POST')\r\n        res = fb.check_session(req)\r\n        self.assertFalse(res)\r\n        req = Request({'fb_sig': fb._hash_args({'in_canvas': '1',\r\n                                                'added': '1',\r\n                                                'expires': '1',\r\n                                                'friends': 'joe,mary',\r\n                                                'session_key': 'abc',\r\n                                                'user': 'bob'}),\r\n                       'fb_sig_in_canvas': '1',\r\n                       'fb_sig_added': '1',\r\n                       'fb_sig_expires': '1',\r\n                       'fb_sig_friends': 'joe,mary',\r\n                       'fb_sig_session_key': 'abc',\r\n                       'fb_sig_user': 'bob'},\r\n                      {}, 'POST')\r\n        res = fb.check_session(req)\r\n        self.assertTrue(res)\r\n        fb = facebook.Facebook(my_api_key, my_secret_key)\r\n        fb.login = self.login\r\n        req = Request({'fb_sig': fb._hash_args({'in_canvas': '1',\r\n                                                'added': '1',\r\n                                                'expires': '1',\r\n                                                'friends': '',\r\n                                                'session_key': 'abc',\r\n                                                'user': 'bob'}),\r\n                       'fb_sig_in_canvas': '1',\r\n                       'fb_sig_added': '1',\r\n                       'fb_sig_expires': '1',\r\n                       'fb_sig_friends': '',\r\n                       'fb_sig_session_key': 'abc',\r\n                       'fb_sig_user': 'bob'},\r\n                      {}, 'POST')\r\n        res = fb.check_session(req)\r\n        self.assertTrue(res)\r\n        fb = facebook.Facebook(my_api_key, my_secret_key)\r\n        fb.login = self.login\r\n        req = Request({'fb_sig': fb._hash_args({'in_canvas': '1',\r\n                                                'added': '1',\r\n                                                'expires': '1',\r\n                                                'friends': '',\r\n                                                'session_key': 'abc',\r\n                                                'page_id': 'id'}),\r\n                       'fb_sig_in_canvas': '1',\r\n                       'fb_sig_added': '1',\r\n                       'fb_sig_expires': '1',\r\n                       'fb_sig_friends': '',\r\n                       'fb_sig_session_key': 'abc',\r\n                       'fb_sig_page_id': 'id'},\r\n                      {}, 'POST')\r\n        res = fb.check_session(req)\r\n        self.assertTrue(res)\r\n\r\n    def test6(self):\r\n        global response_str\r\n        response = 'abcdef'\r\n        response_str = simplejson.dumps(response)\r\n        fb = facebook.Facebook(my_api_key, my_secret_key)\r\n        fb.login = self.login\r\n        fb.auth.createToken()\r\n#        self.failUnlessRaises(RuntimeError,fb._add_session_args)\r\n        response = {\r\n            \"session_key\": \"key\",\r\n            \"uid\": \"my_uid\",\r\n            \"secret\": \"my_secret\",\r\n            \"expires\": \"my_expires\"}\r\n        response_str = simplejson.dumps(response)\r\n        fb.auth.getSession()\r\n        args = fb._add_session_args()\r\n\r\n    def test7(self):\r\n        global response_str\r\n        response = 'abcdef'\r\n        response_str = simplejson.dumps(response)\r\n        fb = facebook.Facebook(my_api_key, my_secret_key)\r\n        fb.login = self.login\r\n        fb.auth.createToken()\r\n        self.assertEquals(str(fb.auth_token), \"abcdef\")\r\n        url = fb.get_authorize_url(next=\"next\", next_cancel=\"next_cancel\")\r\n        self.assertEquals(\r\n            url,\r\n            'http://www.facebook.com/authorize.php?api_key=%s&next_cancel=next_cancel&v=1.0&next=next' %\r\n            (my_api_key,\r\n             ))\r\n\r\n    def test8(self):\r\n        class Request:\r\n\r\n            def __init__(self, post, get, method):\r\n                self.POST = post\r\n                self.GET = get\r\n                self.method = method\r\n\r\n        global response_str\r\n        response = {\r\n            \"session_key\": \"abcdef\",\r\n            \"uid\": \"my_uid\",\r\n            \"secret\": \"my_secret\",\r\n            \"expires\": \"my_expires\"}\r\n        response_str = simplejson.dumps(response)\r\n        req = Request({}, {'installed': 1, 'fb_page_id': 'id',\r\n                           'auth_token': 'abcdef'}, 'GET')\r\n        fb = facebook.Facebook(my_api_key, my_secret_key)\r\n        fb.login = self.login\r\n        res = fb.check_session(req)\r\n        self.assertTrue(res)\r\n\r\n    def test9(self):\r\n        global response_str\r\n        response = 'abcdef'\r\n        response_str = simplejson.dumps(response)\r\n        fb = facebook.Facebook(my_api_key, my_secret_key)\r\n        fb.login = self.login\r\n        fb.auth.createToken()\r\n        self.assertEquals(str(fb.auth_token), \"abcdef\")\r\n        url = fb.get_add_url(next=\"next\")\r\n        self.assertEquals(\r\n            url, 'http://www.facebook.com/install.php?api_key=%s&v=1.0&next=next' %\r\n            (my_api_key,))\r\n\r\n    def send(self, xml):\r\n        self.xml = xml\r\n\r\n    def test10(self):\r\n        import Image\r\n        image1 = Image.new(\"RGB\", (400, 300), (255, 255, 255))\r\n        filename = \"image_file.jpg\"\r\n        image1.save(filename)\r\n        global response_str\r\n        fb = facebook.Facebook(my_api_key, my_secret_key)\r\n        fb.login = self.login\r\n\r\n        facebook.httplib.HTTP = Mock('httplib.HTTP')\r\n        http_connection = Mock('http_connection')\r\n        facebook.httplib.HTTP.mock_returns = http_connection\r\n        http_connection.send.mock_returns_func = self.send\r\n\r\n        def _http_passes():\r\n            return [200, ]\r\n        http_connection.getreply.mock_returns_func = _http_passes\r\n\r\n        def read():\r\n            response = {\"stuff\": \"stuff\"}\r\n            response_str = simplejson.dumps(response)\r\n            return response_str\r\n        http_connection.file.read.mock_returns_func = read\r\n\r\n        response = {\r\n            \"session_key\": \"key\",\r\n            \"uid\": \"my_uid\",\r\n            \"secret\": \"my_secret\",\r\n            \"expires\": \"my_expires\"}\r\n        response_str = simplejson.dumps(response)\r\n        res = fb.auth.getSession()\r\n        result = fb.photos.upload(\r\n            image=filename,\r\n            aid=\"aid\",\r\n            caption=\"a caption\")\r\n        self.assertEquals(str(result[\"stuff\"]), \"stuff\")\r\n        os.remove(filename)\r\n\r\nif __name__ == \"__main__\":\r\n\r\n    # Build the test suite\r\n    suite = unittest.TestSuite()\r\n    suite.addTest(unittest.makeSuite(pyfacebook_UnitTests))\r\n\r\n    # Execute the test suite\r\n    print(\"Testing Proxy class\\n\")\r\n    result = unittest.TextTestRunner(verbosity=2).run(suite)\r\n    sys.exit(len(result.errors) + len(result.failures))\r\n"
  }
]