Reading facebook JSON object "signed_request"

I'm designing a Facebook canvas app, and am just at the beginning.  To test that I can communicate with Facebook correctly at all, I am trying to read the JSON object that Facebook sends to my page, named "signed_request".  Every method I try to use to read it claims that the base64 JSON object data is invalid.  For example, using the JSON.net library, in vb.net, with this code:
Dim mystring As String
mystring = JsonConvert.DeserializeObject(request.form("signed_request"))

I get this error:
Newtonsoft.Json.JsonReaderException: Error parsing boolean value. Line 1, position 2.

Or, when using built-in string conversion functions with this code:
Dim b As Byte() = Convert.FromBase64String(request.form("signed_request"))
mystring = System.Text.Encoding.UTF8.GetString(b)

I get:
System.FormatException: Invalid character in a Base-64 string.

What am I doing wrong?
CarbaseAsked:
Who is Participating?
 
Bob LearnedConnect With a Mentor Commented:
If I understand what "signed_request" is, it is a Base-64 encoded URL.

You could use the HttpServerUtility.UrlTokenDecode method decode a Base-64 URL.

Code for decoding/encoding a modified base64 URL
http://stackoverflow.com/questions/1228701/code-for-decoding-encoding-a-modified-base64-url

''<summary>
''' Base 64 Encoding with URL and Filename Safe Alphabet using UTF-8 character set.
'''</summary>
'''<param name="str">The origianl string</param>
'''<returns>The Base64 encoded string</returns>
Public Shared Function Base64Encode(str As String) As String
	Dim encbuff As Byte() = Encoding.UTF8.GetBytes(str)
	Return HttpServerUtility.UrlTokenEncode(encbuff)
End Function

'''<summary>
''' Decode Base64 encoded string with URL and Filename Safe Alphabet using UTF-8.
'''</summary>
'''<param name="str">Base64 code</param>
'''<returns>The decoded string.</returns>
Public Shared Function Base64Decode(str As String) As String
	Dim decbuff As Byte() = HttpServerUtility.UrlTokenDecode(str)
	Return Encoding.UTF8.GetString(decbuff)
End Function

Open in new window

0
 
CarbaseAuthor Commented:
It is mostly a Base-64 encoded URL, but I found out they prepended some other info to it, separating the two sections with a dot.  Once I looked just after the dots, and padded the length when necessary, everything was fine.

Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.