def process_request(self, request): """ Writes the signed_request into the Session """ fb = get_session(request) setattr(request, 'fb_session', fb) application = get_app_dict() logger.debug('Request Method = %s\n, AccessToken=%s' % (request.method, fb.access_token)) if 'feincms' in settings.INSTALLED_APPS: # if feincms is installed, try to get the application from the page from facebook.feincms.utils import get_application_from_request page_app = get_application_from_request(request) if application: application = get_app_dict(page_app) # Temporary OAuth2.0 fix due to missing access_token in cookie sr: if 'access_token' in request.GET: fb.store_token(request.GET.get('access_token')) # default POST/GET request from facebook with a signed request if 'signed_request' in request.POST: parsed_request = parseSignedRequest(request.POST['signed_request'], application['SECRET']) logger.debug(u'got signed_request from facebook: %s' % parsed_request) if 'user' in parsed_request: language = parsed_request['user']['locale'] logger.debug('language: %s' %language) request.LANGUAGE_CODE = language translation.activate(language) fb.signed_request = parsed_request logger.debug('stored signed_request') expires = None # rewrite important data if 'oauth_token' in parsed_request: expires = datetime.fromtimestamp(float(parsed_request['expires'])) fb.store_token(parsed_request['oauth_token'], expires) elif 'access_token' in parsed_request: expires = datetime.fromtimestamp(float(parsed_request['expires'])) fb.store_token(parsed_request['access_token'], expires) else: #The chance is good that there is already a valid token in the session. Remove it. fb.store_token(None) if 'user_id' in parsed_request: fb.user_id = parsed_request['user_id'] else: logger.debug("Signed Request didn't contain public user info.") if expires: logger.debug('Signed Request issued at: %s' % datetime.fromtimestamp(float(parsed_request['issued_at']))) # auth via callback from facebook elif 'code' in request.GET and 'facebook' in request.META.get('HTTP_REFERER', u''): authenticate(request.REQUEST['code'], fb, application, request.build_absolute_uri().split('?')[0] \ .replace(application['CANVAS-URL'], application['CANVAS-PAGE']))
def send_to_facebook(self, object='me', save=False, graph=None, message=None, app_name=None): if not graph: graph = get_graph(app_name=app_name) if not message: message = self.message app_dict = get_app_dict(app_name) #response = post_image(graph.access_token, self.image.file, message, object=object) #response = graph.put_photo(self.image.file, message=message) image_url = 'http://%s%s' % (app_dict['DOMAIN'], self.image.url) logger.debug('image_url: %s' % image_url) response = graph.put_photo_url(image_url, message, object) logger.debug('response: %s' % response) if save: self.fb_id = response['id'] self.slug = response['id'] self.save() return response['id']
def send_to_facebook(self, object='me', save=False, graph=None, message=None, app_name=None): if not graph: graph = get_graph(app_name=app_name) if not message: message = self.message app_dict = get_app_dict(app_name) #response = post_image(graph.access_token, self.image.file, message, object=object) #response = graph.put_photo(self.image.file, message=message) image_url = 'http://%s%s' % (app_dict['DOMAIN'], self.image.url) logger.debug('image_url: %s' % image_url ) response = graph.put_photo_url(image_url, message, object) logger.debug('response: %s' % response ) if save: self.fb_id = response['id'] self.slug = response['id'] self.save() return response['id']
def process_request(self, request): """ Writes the signed_request into the Session """ fb = FBSession(request) application = get_app_dict() if "feincms" in settings.INSTALLED_APPS: # if feincms is installed, try to get the application from the page from facebook.feincms.utils import get_application_from_request page_app = get_application_from_request(request) if application: application = get_app_dict(page_app) # default POST/GET request from facebook with a signed request if "signed_request" in request.POST: parsed_request = parseSignedRequest(request.POST["signed_request"], application["SECRET"]) logger.debug(u"got signed_request from facebook: %s" % parsed_request) if "language" in parsed_request: language = parsed_request["user"]["locale"] logger.debug("language: %s" % language) request.LANGUAGE_CODE = language translation.activate(language) fb.signed_request = parsed_request logger.debug("stored signed_request") expires = None # rewrite important data if "oauth_token" in parsed_request: expires = datetime.fromtimestamp(float(parsed_request["expires"])) fb.store_token(parsed_request["oauth_token"], expires) elif "access_token" in parsed_request: expires = datetime.fromtimestamp(float(parsed_request["expires"])) fb.store_token(parsed_request["access_token"], expires) else: # The chance is good that there is already a valid token in the session. fb.store_token(None) if "user_id" in parsed_request: fb.user_id = parsed_request["user_id"] else: logger.debug("Signed Request didn't contain public user info.") if expires: logger.debug( "Signed Request issued at: %s" % datetime.fromtimestamp(float(parsed_request["issued_at"])) ) # auth via callback from facebook elif "code" in request.REQUEST: args = dict( client_id=application["id"], client_secret=application["secret"], code=request.REQUEST["code"], redirect_uri=request.build_absolute_uri() .split("?")[0] .replace(application["CANVAS-URL"], application["CANVAS-PAGE"]), ) response = urllib.urlopen("https://graph.facebook.com/oauth/access_token?" + urllib.urlencode(args)) raw = response.read() parsed = urlparse.parse_qs(raw) # Python 2.6 parse_qs is now part of the urlparse module if parsed.get("access_token", None): expires = datetime.fromtimestamp(float(parsed["expires"][-1])) fb.store_token(parsed["access_token"][-1], expires) logger.debug("Got access token from callback: %s. Expires at %s" % (parsed, expires)) else: logger.debug("facebook did not respond an accesstoken: %s" % raw)
def process_request(self, request): """ Writes the signed_request into the Session """ fb = get_session(request) setattr(request, 'fb_session', fb) application = get_app_dict() logger.debug('Request Method = %s\n, AccessToken=%s' % (request.method, fb.access_token)) if 'feincms' in settings.INSTALLED_APPS: # if feincms is installed, try to get the application from the page from facebook.feincms.utils import get_application_from_request page_app = get_application_from_request(request) if application: application = get_app_dict(page_app) # Temporary OAuth2.0 fix due to missing access_token in cookie sr: if 'access_token' in request.GET: fb.store_token(request.GET.get('access_token')) # default POST/GET request from facebook with a signed request if 'signed_request' in request.POST: parsed_request = parseSignedRequest(request.POST['signed_request'], application['SECRET']) logger.debug(u'got signed_request from facebook: %s' % parsed_request) # if 'user' in parsed_request: # language = parsed_request['user']['locale'] # logger.debug('language: %s' %language) # request.LANGUAGE_CODE = language # translation.activate(language) fb.signed_request = parsed_request logger.debug('stored signed_request') expires = None # rewrite important data if 'oauth_token' in parsed_request: expires = datetime.fromtimestamp( float(parsed_request['expires'])) fb.store_token(parsed_request['oauth_token'], expires) elif 'access_token' in parsed_request: expires = datetime.fromtimestamp( float(parsed_request['expires'])) fb.store_token(parsed_request['access_token'], expires) else: #The chance is good that there is already a valid token in the session. Remove it. fb.store_token(None) if 'user_id' in parsed_request: fb.user_id = parsed_request['user_id'] else: logger.debug("Signed Request didn't contain public user info.") if expires: logger.debug( 'Signed Request issued at: %s' % datetime.fromtimestamp(float(parsed_request['issued_at']))) # auth via callback from facebook elif 'code' in request.GET and 'facebook' in request.META.get( 'HTTP_REFERER', u''): authenticate(request.REQUEST['code'], fb, application, request.build_absolute_uri().split('?')[0] \ .replace(application['CANVAS-URL'], application['CANVAS-PAGE']))