Created
March 6, 2012 19:59
-
-
Save jmccartie/1988667 to your computer and use it in GitHub Desktop.
Parse the Facebook "signed_request" param
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Found here: http://stackoverflow.com/questions/4987772/decoding-facebooks-signed-request-in-ruby-sinatra | |
require 'base64' | |
require 'openssl' | |
require 'crack/json' | |
def base64_url_decode str | |
encoded_str = str.gsub('-','+').gsub('_','/') | |
encoded_str += '=' while !(encoded_str.size % 4).zero? | |
Base64.decode64(encoded_str) | |
end | |
def decode_data(signed_request) | |
encoded_sig, payload = signed_request.split('.') | |
data = base64_url_decode(payload) | |
end | |
json_request = decode_data(params[:signed_request]) | |
signed_request = Crack::JSON.parse(json_request) | |
# I needed the profile_id | |
fb_id = signed_request["profile_id"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment