0

我有以下代码:

def base64_url_decode(inp):
    padding_factor = (4 - len(inp) % 4) % 4
    inp += "="*padding_factor 
    return base64.b64decode(unicode(inp).translate(dict(zip(map(ord, u'-_'), u'+/'))))

def parse_signed_request(signed_request, secret):

    l = signed_request.split('.', 2)
    encoded_sig = l[0]
    payload = l[1]

    sig = base64_url_decode(encoded_sig)
    data = json.loads(base64_url_decode(payload))

    if data.get('algorithm').upper() != 'HMAC-SHA256':
        log.error('Unknown algorithm')
        return None
    else:
        expected_sig = hmac.new(secret, msg=payload, digestmod=hashlib.sha256).digest()

    if sig != expected_sig:
        return None
    else:
        log.debug('valid signed request received..')
        return data

如何使用 facebook 签名的请求数据(从 parse_signed_request 返回)来获取此人的电子邮件地址?

这是它的 facebook 文档: https ://developers.facebook.com/docs/howtos/login/signed-request/

我试着做:

data = parsed_signed_request(...)
data.get('registration').email

但这没有用。

我能做些什么?

4

1 回答 1

0

也许你可以试试这个,这是我的代码的一部分:

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'))
于 2013-03-05T16:40:39.603 回答