3

I've been trying to incorporate the Facebook JavaScript SDK into my Google Apps Script. In my script, I display an html page using google's HtmlService, and I've inserted the Facebook tutorial code into said html page.

Google script:

function myFunction() {
    FormApp.getUi().showModalDialog(createHtml('htmlPage'), 'title');
}

function createHtml(fileName) {
  return HtmlService
      .createTemplateFromFile(fileName)
      .evaluate()
      .setSandboxMode(HtmlService.SandboxMode.IFRAME);
}

HTML page:

<!DOCTYPE html>
<html>
  <body>
  <script>
   window.fbAsyncInit = function() {
    FB.init({
      appId      : 'REMOVED',
      xfbml      : true,
      version    : 'v2.6'
    });

    document.write("testing");
  };

  (function(d, s, id){
     var js, fjs = d.getElementsByTagName(s)[0];
     if (d.getElementById(id)) {return;}
     js = d.createElement(s); js.id = id;
     js.src = "//connect.facebook.net/en_US/sdk.js";
     fjs.parentNode.insertBefore(js, fjs);
   }(document, 'script', 'facebook-jssdk'));

   </script>

  <div
    class="fb-like"
    data-share="true"
    data-width="450"
    data-show-faces="true">
  </div>


  </body>
</html>

My problem is that this setup silently fails. Interestingly enough, the document.write() function is called, but the div for the like button isn't rendered, nor have I been able to use any other FB SDK functionality.

What I assume is going on is that the FB SDK requires some sort of Apps Script permissions from the user, which aren't being requested due to the code not being in a .gs file. Utilizing the SDK from a .gs file also doesn't work, due to the SDK being designed for a proper webpage, meaning it would throw things like "window" is not defined".

How do I fix my above setup?

EDIT: I've found this functioning approach (https://stackoverflow.com/a/23300702/2662812), but it seems way too complicated and I'd much rather use the FB JS SDK if possible.

Community
  • 1
  • 1

0 Answers0