Facebook Signed Request Email
I have the following code: def base64_url_decode(inp): padding_factor = (4 - len(inp) % 4) % 4 inp += '='*padding_factor return base64.b64decode(unicode(inp).translate
Solution 1:
Maybe you could try this one, this is part of my codes:
def parse_signed_request(signed_request, app_secret):
try:
l = signed_request.split('.', 2)
encoded_sig = str(l[0])
payload = str(l[1])
except IndexError:
raise ValueError("'signed_request' malformed")
sig = base64.urlsafe_b64decode(encoded_sig + "=" * ((4 - len(encoded_sig) % 4) % 4))
data = base64.urlsafe_b64decode(payload + "=" * ((4 - len(payload) % 4) % 4))
data = json.loads(data)
if data.get('algorithm').upper() != 'HMAC-SHA256':
raise ValueError("'signed_request' is using an unknown algorithm")
else:
expected_sig = hmac.new(app_secret, msg=payload, digestmod=hashlib.sha256).digest()
if sig != expected_sig:
raise ValueError("'signed_request' signature mismatch")
else:
return data
def fb_registration(request):
if request.POST:
if 'signed_request' in request.POST:
# parse and check data
data = parse_signed_request(request.POST['signed_request'], settings.FACEBOOK_APP_SECRET)
# lets try to check if user exists based on username or email
try:
check_user = User.objects.get(username=data['registration']['name'])
except:
state = "Username is already exist. Please try other account."
return HttpResponseRedirect(reverse('accounts:register'))
try:
check_user = User.objects.get(email=data['registration']['email'])
except:
state = "Email is already exist. Please use other account."
return HttpResponseRedirect(reverse('accounts:register'))
#lets create now the user
randompass = ''.join([choice('1234567890qwertyuiopasdfghjklzxcvbnm') for i in range(7)])
user = User.objects.create_user(data['registration']['name'], data['registration']['email'], randompass)
user.save()
user = authenticate(username=data['registration']['name'], password=randompass)
if user is not None:
# save in user profile his facebook id
fbid = 'http://www.facebook.com/profile.php?id=%s' % data['user_id']
r = RPXAssociation(user=user, identifier=fbid)
r.save()
login(request, user)
return HttpResponseRedirect(reverse('accounts:choose_plan'))
else:
state = "Registration request failed!"
return HttpResponseRedirect(reverse('accounts:register'))
Post a Comment for "Facebook Signed Request Email"