2
votes

I am trying to decode the signed request that comes from Facebook. I tried to use the below code using javascript, but I am not sure about the function to use for decoding and then parsing the JSON response. My Response object is signed_request and it contains the user profile information like name, gender, email, etc. I want to do something similar like this page shows in php: (Goto the section which says parsing the signed request) https://developers.facebook.com/docs/facebook-login/using-login-with-games/

   <script>
     $(document).ready(function () {    
       var signedRequest = $("h2").html();
       var data = signedRequest.split('.')[1];       
       data = JSON.parse(data);
       name = data.name;
       console.log(name);
     });

   </script>

 <body>
   <form id="form1" runat="server">
       <div>
        <% string name1 = Request.Params["signed_request"];%>
           <h2><%= name1 %></h2>
       </div>
   </form>
  </body>
2
can anyone help me with this? - newguy
Signed requests are base64 encoded (a version which is url safe), and you can follow the php description here - developers.facebook.com/docs/facebook-login/… - Sean Kinsey
hey have you managed how to decode the signedRequest? can you tell me how? Thanks - Marco Caltagirone

2 Answers

1
votes

According to facebook, we need to:

  1. Split the signed request into two parts delineated by a '.' character (eg. 238fsdfsd.oijdoifjsidf899)
  2. Decode the first part - the encoded signature - from base64url
  3. Decode the second part - the payload - from base64url and then decode the resultant JSON object

One method for parsing base64url in JavaScript is window.atob:

FB.getLoginStatus(res => {
  console.log(res)
  const parts = res.authResponse.signedRequest.split('.')
  const signature = window.atob(parts[0])
  const payload = window.atob(parts[1])
  console.log(signature, JSON.parse(payload))
});
0
votes

Signed requests are base64 encoded (a version which is url safe), and you can follow the php description here - https://developers.facebook.com/docs/facebook-login/using-login-with-games/

If you're extremely interested, you can look at how this is done in the JS SDK by searching through http://connect.facebook.net/en_US/all/debug.js for 'sdk.SignedRequest'.