Backendless Support
 
Waiting for user's response

[REST] Facebook Login return HTML response

Hello,

In my cordova app I use the native facebook plugin to handle all the authorization token and then I call the Login With Facebook SDK. I'm not using the JS SDK because it does not works inside a cordova app (ONLY social login does not work)

The login works perfectly in fact the user is created on the db. However the response of the REST API is a JSON inside an HTML page, so I've to parse the HTML to extract user information.

This is the way I call the REST API

  1. var xhr = new XMLHttpRequest();
  2. xhr.open("POST", "https://api.backendless.com/v1/users/social/facebook/sdk/login";, true);
  3. xhr.setRequestHeader('application-id', Backendless.applicationId);
  4. xhr.setRequestHeader('secret-key', Backendless.secretKey);
  5. xhr.setRequestHeader('Content-Type', 'application/json');
  6. xhr.setRequestHeader('application-type', 'REST');
  7. xhr.onreadystatechange = function() {
  8. if (this.readyState == 4 && this.status == 200) {
  9. console.log(this);
  10. }
  11. };
  12. var body = {
  13. accessToken: params.accessToken,
  14. fieldsMapping: params.facebookFieldsMapping
  15. };
  16. xhr.send(JSON.stringify(body));

This is the response

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <script type="text/javascript">
  5. window.setTimeout( function ()
  6. {
  7. if(window.opener)
  8. window.opener.postMessage( JSON.stringify( {"birthday":"11/13/1997","image":null,"lastLogin":"Fri Dec 09 12:43:52 UTC 2016","gender":"male","created":"Fri Dec 09 12:43:52 UTC 2016","ownerId":"XXXXXXXXXX","__meta":"{\"relationRemovalIds\":{},\"selectedProperties\":[\"birthday\",\"image\",\"gender\",\"created\",\"ownerId\",\"__updated__meta\",\"password\",\"name\",\"___class\",\"id\",\"updated\",\"objectId\",\"email\"],\"relatedObjects\":{}}","user-registered":true,"name":"XXXXXXXXXXXXXXXXX","___class":"Users","id":"XXXXXXXXXXXXXX","user-token":"XXXXXXXXXXXXXXXXXXXXXX","updated":null,"objectId":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX","email":"XXXXXXXXXXXXXXXXXXXXX"} ), 'null' );
  9. else
  10. parent.postMessage( JSON.stringify( {"birthday":"11/13/1997","image":null,"lastLogin":"Fri Dec 09 12:43:52 UTC 2016","gender":"male","created":"Fri Dec 09 12:43:52 UTC 2016","ownerId":"XXXXXXXXXXXXXXXXXXXXXXXXXXXX","__meta":"{\"relationRemovalIds\":{},\"selectedProperties\":[\"birthday\",\"image\",\"gender\",\"created\",\"ownerId\",\"__updated__meta\",\"password\",\"name\",\"___class\",\"id\",\"updated\",\"objectId\",\"email\"],\"relatedObjects\":{}}","user-registered":true,"name":"XXXXXXXXXXXXXXXX","___class":"Users","id":"XXXXXXXXXXXXX","user-token":"XXXXXXXXXXXXXXXXXXXXX","updated":null,"objectId":"XXXXXXXXXXXXXXXXXXXXXXXX","email":"XXXXXXXXXXXXXXXXXXX"} ), 'null' );
  11. }, 0 );
  12. </script>
  13. </body>
  14. </html>"

Any idea how to fix?

Leave a Comment

Comments (4)

photo
1

I did a very dirty solution to fix it...

first i created an invisible iFrame, then I receive the response from Backendless, I replace all occurencies of the origin (that is 'null') with my local origin.

After that I create a listener for the message, I put the modified response on the iFrame, I wait for the message (handling the origin in case is a browser or a mobile device), parse it, extract the user and save it....

It is a very dirty solution so if you can help to find a better one I would be grateful :)

Anyway this is the solution I found

  1. <iframe height="0px" id="fbContentIframe" style="display: none;" width="0px">
  2. </iframe>

  1. function makeBackendlessFbLogin(params, success, error) {
  2. var xhr = new XMLHttpRequest();
  3. xhr.open("POST", "https://api.backendless.com/v1/users/social/facebook/sdk/login";, true);
  4. xhr.setRequestHeader('application-id', Backendless.applicationId);
  5. xhr.setRequestHeader('secret-key', Backendless.secretKey);
  6. xhr.setRequestHeader('Content-Type', 'application/json');
  7. xhr.setRequestHeader('application-type', 'REST');
  8. xhr.onreadystatechange = function() {
  9. if (this.readyState == 4 && this.status == 200) {
  10. //console.log(this);
  11. var iframe = $window.document.getElementById('fbContentIframe');
  12. _addMessageListener(success);
  13. iframe.contentWindow.document.open('text/htmlreplace');
  14. var html;
  15. if ($window.cordova.platformId == "browser") {
  16. html = this.response.replace(/\'null\' \)\;/g, "'http://localhost:8000');");
  17. } else {
  18. html = this.response.replace(/\'null\' \)\;/g, "'file://');");
  19. }
  20. iframe.contentWindow.document.write(html);
  21. iframe.contentWindow.document.close();
  22. console.log(iframe);
  23. //
  24. }
  25. };
  26. var body = {
  27. accessToken: params.accessToken,
  28. fieldsMapping: params.facebookFieldsMapping
  29. };
  30. xhr.send(JSON.stringify(body));
  31. }
  32. function _getUserFromResponse(user) {
  33. Backendless.LocalCache.set("current-user-id", user.objectId);
  34. var newUser = new Backendless.User();
  35. for (var i in user) {
  36. if (user.hasOwnProperty(i)) {
  37. if (i == 'user-token') {
  38. if (Backendless.LocalCache.get("stayLoggedIn")) {
  39. Backendless.LocalCache.set("user-token", user);
  40. }
  41. continue;
  42. }
  43. newUser = user;
  44. }
  45. }
  46. return newUser;
  47. }
  48. function _addMessageListener(success) {
  49. Backendless.Utils.addEvent('message', $window, function(e) {
  50. console.log("RECEIVED MESSAGE:");
  51. console.log(e);
  52. var origin;
  53. if ($window.cordova.platformId == "browser") {
  54. origin = 'http://localhost:8000';
  55. } else {
  56. origin = 'file://';
  57. }
  58. if (e.origin == origin) {
  59. try {
  60. var result = JSON.parse(e.data);
  61. if (result && result.objectId && result.name) {
  62. console.log(result);
  63. Backendless.LocalCache.set("stayLoggedIn", true);
  64. success();
  65. _getUserFromResponse(result);
  66. }
  67. Backendless.Utils.removeEvent('message', $window);
  68. } catch (e) {}
  69. }
  70. });
  71. }

photo
2

Hi Michele,

thank you for the temporary solution, maybe it helps other users, but anyway we will investigate why you receive HTML instead of json and will resolve this issue. Thanks for patience.

Regards

Stanislav

photo
1

Hello Stanislaw,

any news about this issue?

for the moment I check each time if the response is JSON or HTML and handle differently in both case, but I hope that will be a fix.

Thanks

photo
1

Hi Michele,

seems you set in `secret-key` header JS secret key. You should set there REST secret key. After that everything should be OK.

Regards,

Stanislaw

photo