Ejemplo n.º 1
0
def get_facebook_api(request):
	"""
		This will return None on failure (we call this from places where we know there's no API -- responsible for checking None condition on other IFs)
	"""
	from jjmaker.models import get_user_profile, new_user_profile
	fb = Facebook(settings.FACEBOOK_API_KEY, settings.FACEBOOK_SECRET_KEY)

	# Use the data from the cookie if present
	try:
		auth_token = get_facebook_access_token(request)
	except:
		auth_token = request.GET.get('auth_token', None)

	try:
		fb.auth_token = auth_token
		userprofile_id = get_facebook_uid(request)
	except KeyError, MultiValueDictKeyError:
		traceback.print_exc()
		fb = None
Ejemplo n.º 2
0
def web_app(request):
    """Get the user's friends and their pictures. This example uses
       the Django web framework, but should be adaptable to others."""

    # Get api_key and secret_key from a file
    fb_file = open('facebook_keys.txt').readlines()
    api_key = fb_file[0].strip()
    secret_key = fb_file[1].strip()
    fb = Facebook(api_key, secret_key)

    # Use the data from the cookie if present
    if 'session_key' in request.session and 'uid' in request.session:
        fb.session_key = request.session['session_key']
        fb.uid = request.session['uid']
    else:
        
        try:
            fb.auth_token = request.GET['auth_token']
        except KeyError:
            # Send user to the Facebook to login
            return HttpResponseRedirect(fb.get_login_url())

        # getSession sets the session_key and uid
        # Store these in the cookie so we don't have to get them again
        fb.auth.getSession()
        request.session['session_key'] = fb.session_key
        request.session['uid'] = fb.uid

    try:
        friend_ids = fb.friends.get()
    except FacebookError, e:
        # Error 102 means the session has expired.
        # Delete the cookie and send the user to Facebook to login
        if e.info['code'] == u'102':
            del request.session['session_key']
            del request.session['uid']
            return HttpResponseRedirect(fb.get_login_url())
        else:
            # Other Facebook errors are possible too. Don't ignore them.
            raise