2013-03-05 55 views
0

我有以下代碼:Facebook網站上簽名請求電子郵件

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 

,但沒有奏效。

我該怎麼辦?

回答

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'))