[web2py] Re: 'Morsel' object has no attribute 'split' ?
Hey web2py_tn, I don't know if you still need this but I recently battled this problem myself and here is a solution I'd like to leave here so others don't need to battle with this too. If you're using the web2py_multiauth first apply this patch: http://code.google.com/p/cfhowes/issues/detail?id=2 Then overwrite these 2 functions in facebook.py to: def parse_signed_request(signed_request, application_secret_key): """Parse a signed request, returning a dictionary describing its payload.""" def decode(encoded): padding = '=' * (len(encoded) % 4) return base64.urlsafe_b64decode(encoded + padding) try: encoded_signature, encoded_payload = (str(string) for string in signed_request.split('.', 2)) signature = decode(encoded_signature) signed_request_data = json.loads(decode(encoded_payload)) except (TypeError, ValueError): raise Exception("Signed request had a corrupt payload") if signed_request_data.get('algorithm', '').upper() != 'HMAC-SHA256': raise Exception("Signed request is using an unknown algorithm") expected_signature = hmac.new(application_secret_key, msg=encoded_payload, digestmod=hashlib.sha256).digest() if signature != expected_signature: raise Exception("Signed request signature mismatch") return signed_request_data def get_user_from_cookie(cookies, app_id, app_secret): """Parses the cookie set by the official Facebook JavaScript SDK. cookies should be a dictionary-like object mapping cookie names to cookie values. If the user is logged in via Facebook, we return a dictionary with the keys "uid" and "access_token". The former is the user's Facebook ID, and the latter can be used to make authenticated requests to the Graph API. If the user is not logged in, we return None. Download the official Facebook JavaScript SDK at http://github.com/facebook/connect-js/. Read more about Facebook authentication at http://developers.facebook.com/docs/authentication/. """ cookie = cookies.get("fbsr_" + app_id, "") if not cookie: return None response = parse_signed_request(cookie.value, app_secret) if not response: return None args = dict( code = response['code'], client_id = app_id, client_secret = app_secret, redirect_uri = '', ) file = urllib.urlopen("https://graph.facebook.com/oauth/access_token?"; + urllib.urlencode(args)) try: token_response = file.read() finally: file.close() access_token = cgi.parse_qs(token_response)["access_token"][-1] return dict( uid = response["user_id"], access_token = access_token, ) The key change here is in get_user_from cookie where "parse_signed_request(cookie, app_secret)" changed to "parse_signed_request(cookie.value, app_secret)" Sexta-feira, 1 de Fevereiro de 2013 22:00:40 UTC, web2py_tn escreveu: > > Hey Ben- > Can you share your version of facebook.py? > > On Wednesday, February 1, 2012 2:24:29 PM UTC+1, Ben Tammetta wrote: >> >> It seemed that version of facebook.py that I was using is just >> incompatible and/or outdated >> get_user_from_cookie() was trying to parse a cookie variable out that did >> not exist from facebook. >> >> I ended up passing the correct cookie variable directly in and changing a >> couple lines to prove that that was the problem. >> When I ran into other issues it seemed best just to write my own version >> of facebook.py as I needed certain functions. >> > -- --- You received this message because you are subscribed to the Google Groups "web2py-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
[web2py] Re: 'Morsel' object has no attribute 'split' ?
Hey Ben- Can you share your version of facebook.py? On Wednesday, February 1, 2012 2:24:29 PM UTC+1, Ben Tammetta wrote: > > It seemed that version of facebook.py that I was using is just > incompatible and/or outdated > get_user_from_cookie() was trying to parse a cookie variable out that did > not exist from facebook. > > I ended up passing the correct cookie variable directly in and changing a > couple lines to prove that that was the problem. > When I ran into other issues it seemed best just to write my own version > of facebook.py as I needed certain functions. > -- --- You received this message because you are subscribed to the Google Groups "web2py-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
[web2py] Re: 'Morsel' object has no attribute 'split' ?
It seemed that version of facebook.py that I was using is just incompatible and/or outdated get_user_from_cookie() was trying to parse a cookie variable out that did not exist from facebook. I ended up passing the correct cookie variable directly in and changing a couple lines to prove that that was the problem. When I ran into other issues it seemed best just to write my own version of facebook.py as I needed certain functions.
[web2py] Re: 'Morsel' object has no attribute 'split' ?
Cant't you just modify the interface code to get the expected string input? It seems that the input data is stored in the Morsel instance. On 29 ene, 04:11, Ben Tammetta wrote: > Hello, > > I am using the code provided here to create a web2py facebook > applicationhttps://github.com/pythonforfacebook/facebook-sdk/blob/master/faceboo... > > When I call this function inside of facebook.py . > testvar = facebook.get_user_from_cookie(request.cookies, FACEBOOK_APP_ID, > FACEBOOK_APP_SECRET) > > I always get this error. > 'Morsel' object has no attribute 'split' > > It is trying to split the signed_request variable that does exits as in > this case which was returned from facebook. > > signed_request fbsr_156579191121738='_idbY3Z4113pO9Z2h...CI6MTMyNzgxOTQ4MSwidXNlcl9pZCI6IjY5NzE1MTYyMiJ9'> > signed_request.split undefined > > I am not sure what I am doing wrong. Any pointers or suggestions? > > Ben > > More details below. > > Ticket ID > > 127.0.0.1.2012-01-29.00-45-53.cca4dcc3-0338-40d3-aaf0-e9ec6a29f163 > 'Morsel' object has no attribute 'split' > Version > web2py™ (1, 99, 4, datetime.datetime(2011, 12, 14, 14, 46, 14), 'stable') > Python Python 2.6.6: /usr/bin/python > > > > Traceback > File "applications/FB_Tab_App/modules/facebook.py", line 376, in > get_user_from_cookie > parsed_request = parse_signed_request(cookie, app_secret) > File "applications/FB_Tab_App/modules/facebook.py", line 395, in > parse_signed_request > l = signed_request.split('.', 2) > AttributeError: 'Morsel' object has no attribute 'split' > > Error snapshot help > > ('Morsel' object has no attribute 'split') > > .. > > Function argument list > > (signed_request= fbsr_156579191121738='_idbY3Z4113pO9Z2h...CI6MTMyNzgxOTQ4MSwidXNlcl9pZCI6IjY5NzE1MTYyMiJ9'>, > app_secret='16dc8c9503dca01b0bf3b36a41e4b43c') > > . > > Code listing > > > information requested in the scope. > > If the signed_request is malformed or corrupted, False is returned. > """ > try: > l = signed_request.split('.', 2) > > encoded_sig = str(l[0]) > payload = str(l[1]) > sig = base64.urlsafe_b64decode(encoded_sig + "=" * ((4 - len(encoded_sig) % > 4) % 4)) > data = base64.urlsafe_b64decode(payload + "=" * ((4 - len(payload) % 4) % > 4)) > > Variables > signed_request fbsr_156579191121738='_idbY3Z4113pO9Z2h...CI6MTMyNzgxOTQ4MSwidXNlcl9pZCI6IjY5NzE1MTYyMiJ9'> > signed_request.split undefined > l undefined