Sign In Register

How can we help you today?

Start a new topic
Answered

Getting data from Facebook (string encodings)

When I try to get email data from Facebook in cloud code, I get this instead of "@": "\u0040"

If I use the same Facebook URL in a browser, I see the @ sign correctly.
I can also just use the "@" character correctly in cloud code, if I just type it myself.


( Also, with some language-specific characters, if I write them in cloud code, they don't work. But if I save them in NoSQL, and load the json from there, then the characters can be sent to the client and appear correctly. )


Could you clarify what causes this, and which string encodings are supported by which parts of the system? 

Is there a method to apply to response data to make sure the strings are decoded correctly?


Best Answer

Hi Baris,


I've found a solution that should be able to parse any unicode characters that come in as strings. I tested this out with different unicode characters including symbols, inflections in characters (á for example) and regular characters.


  

var x= responseJson.email;  //"somestring\\u01E1\\u0040"

var r = /\\u([\d\w]{4})/gi;  //Regular expression that should handle what part to replace

x = x.replace(r, function (match, grp) {
    return String.fromCharCode(parseInt(grp, 16)); } );

x = unescape(x);  // x is now "somestringǡ@"

  


You can wrap this into a module to pass into whenever you need to convert from unicode.


Give it a try and let me know if that works out for you.


-Pádraig


Great to hear Baris!


If you have any more questions feel free to let me know.


-Pádraig

This worked well, thanks!

Answer

Hi Baris,


I've found a solution that should be able to parse any unicode characters that come in as strings. I tested this out with different unicode characters including symbols, inflections in characters (á for example) and regular characters.


  

var x= responseJson.email;  //"somestring\\u01E1\\u0040"

var r = /\\u([\d\w]{4})/gi;  //Regular expression that should handle what part to replace

x = x.replace(r, function (match, grp) {
    return String.fromCharCode(parseInt(grp, 16)); } );

x = unescape(x);  // x is now "somestringǡ@"

  


You can wrap this into a module to pass into whenever you need to convert from unicode.


Give it a try and let me know if that works out for you.


-Pádraig

Thanks Padrag,


This is how we do it ( "CompileEmailList" module in our cloud code):


            var httpSender = Spark.getHttp("https://graph.facebook.com/me?fields=name,email&access_token=" + token);

            var response = httpSender.get();

            var responseJson = response.getResponseJson();

            

            var email = responseJson.email;

            var playerName = responseJson.name;

            

            if (email) {

                email = email.replace("\\u0040", "@");

                var doc = { "playerId": playerId, "name": playerName, "email": email};

                Spark.runtimeCollection(outputCollection).update({ "playerId": playerId}, doc, true, false);

            }


Before we did the email.replace, \u0040 appeared as a string. So that's a workaround for the @ problem, but I think we'll still have problems with things like uncommon characters in peoples' names. 


Hey Baris,


Can you tell me more about how you're getting the email from facebook and how you're sending it to Cloud Code?


Just want to test it as much as I can. When I type the unicode "\u0040" as input in the Test Harness it gets converted properly to the @ symbol.


I'll keep looking into this on my end.


-Pádrag

Not sure how that happened Baris,


Will get back to you shortly.


Oisin



This changed status to "Answered" without getting an answer

Thanks for pointing this out Baris


The team will investigate.

Login to post a comment