how to read facebook signed_request to get user_id

后端 未结 3 1094
陌清茗
陌清茗 2021-01-19 02:25

According to Facebook - Authentication within a Canvas Page Document, they say that we will be getting a signed_request which consists a JSON object. Now they s

3条回答
  •  無奈伤痛
    2021-01-19 03:29

    If you don't want to work with the FB SDK you can use this snippet of code to get the user_id and other variables (snippet from https://developers.facebook.com/docs/facebook-login/using-login-with-games/)

    function parse_signed_request($signed_request) {
      list($encoded_sig, $payload) = explode('.', $signed_request, 2); 
    
      // decode the data
      $sig = base64_url_decode($encoded_sig);
      $data = json_decode(base64_url_decode($payload), true);
    
      // confirm the signature
      $expected_sig = hash_hmac('sha256', $payload, $secret, $raw = true);
      if ($sig !== $expected_sig) {
        error_log('Bad Signed JSON signature!');
        return null;
      }
    
      return $data;
    }
    
    function base64_url_decode($input) {
      return base64_decode(strtr($input, '-_', '+/'));
    }
    

提交回复
热议问题