Playfab-ReferenceError:未定义“ Promise”

时间:2019-04-22 02:10:20

标签: javascript asynchronous google-apps-script

我正在Google Spreadsheets中创建一个新的小项目,以从Playfab服务器获取一些数据,以进行Kongregate中的游戏。 Playfab提供了一个Javascript API来工作:

https://download.playfab.com/PlayFabClientApi.js

我将使用功能

但是当我尝试运行我的第一个测试时,我得到了错误消息:

ReferenceError: "Promise" no está definido. (línea 33, archivo "Código")

经过一些研究,我读到GAS(Google Apps Script)不支持Promise,但是我读过V8的某个地方可以使用Promise ...我有点迷失了,您能帮我些忙吗?< / p>

我在项目中的代码:

// Load JavaScript from External Server
var url = "https://download.playfab.com/PlayFabClientApi.js";
var javascript = UrlFetchApp.fetch(url).getContentText();
var token = "1111111111111111111111111111111111111111111111111111111111111111";
var kongID = "1111111";
eval(javascript);
/* ######################################################################## */
/* ######################## MENU FUNCTION ################################# */
/* ######################################################################## */
function onOpen(){
  var menu = SpreadsheetApp.getUi().createMenu('PLAYFAB MENU');

  menu.addItem('FirstCallPlayfab', 'PlayFabAPICall')
  .addToUi(); 
}

function PlayFabAPICall() {
  PlayFab.settings.titleId = "E3FA";
  var loginRequest = {
    // Currently, you need to look up the correct format for this object in the API-docs:
    // https://api.playfab.com/documentation/Client/method/LoginWithCustomID
    TitleId: PlayFab.settings.titleId,
    AuthTicket: token,
    CreateAccount: false,
    KongregateId: kongID,
  };

  PlayFabClientSDK.LoginWithKongregate(loginRequest, LoginCallback);
}

var LoginCallback = function (result, error) {
  if (result !== null) {
    Logger.log("Congratulations, you made your first successful API call!");
  }
  else if (error !== null) {
    Logger.log("Something went wrong with your first API call.\n" +
    "Here's some debug information:\n" +
    PlayFab.GenerateErrorReport(error));
  }
}

API文件中的函数LoginWithKongregate:

LoginWithKongregate: function (request, callback, customData, extraHeaders) {
    request.TitleId = PlayFab.settings.titleId ? PlayFab.settings.titleId : request.TitleId; if (!request.TitleId) throw PlayFab._internalSettings.errorTitleId;
    // PlayFab._internalSettings.authenticationContext can be modified by other asynchronous login attempts
    // Deep-copy the authenticationContext here to safely update it
    var authenticationContext = JSON.parse(JSON.stringify(PlayFab._internalSettings.authenticationContext));
    var overloadCallback = function (result, error) {
        if (result != null) {
            if(result.data.SessionTicket != null) {
                PlayFab._internalSettings.sessionTicket = result.data.SessionTicket;
            }
            if (result.data.EntityToken != null) {
                PlayFab._internalSettings.entityToken = result.data.EntityToken.EntityToken;
            }
            // Apply the updates for the AuthenticationContext returned to the client
            authenticationContext = PlayFab._internalSettings.UpdateAuthenticationContext(authenticationContext, result);
            PlayFab.ClientApi._MultiStepClientLogin(result.data.SettingsForUser.NeedsAttribution);
        }
        if (callback != null && typeof (callback) === "function")
            callback(result, error);
    };
    PlayFab._internalSettings.ExecuteRequestWrapper("/Client/LoginWithKongregate", request, null, overloadCallback, customData, extraHeaders);
    // Return a Promise so that multiple asynchronous calls to this method can be handled simultaneously with Promise.all()
    return new Promise(function(resolve){resolve(authenticationContext);});
},

1 个答案:

答案 0 :(得分:1)

  • 您要使用PlayFabAPICall()运行https://download.playfab.com/PlayFabClientApi.js的脚本。
  • 您要从自定义菜单运行脚本。

如果我的理解是正确的,那么该示例脚本如何?不幸的是,在当前阶段,服务器端的Google Apps脚本尚不能使用“ Promise”。因此,作为当前解决方法,我建议使用自定义对话框和边栏。在此示例脚本中,使用了自定义对话框。该脚本的流程如下。请认为这只是几个答案之一。

  1. 打开电子表格。
  2. 从自定义菜单运行openDialog()脚本。
  3. 打开一个对话框,运行index.html
    • 在这种情况下,index.html在您的浏览器上运行。

示例脚本:

请复制以下脚本并将其粘贴到脚本编辑器中。 Code.gsindex.html分别是脚本和html。

Code.gs:Google Apps脚本

function onOpen(){
  var menu = SpreadsheetApp.getUi().createMenu('PLAYFAB MENU');
  menu.addItem('FirstCallPlayfab', 'openDialog').addToUi(); 
}

function openDialog() {
  var html = HtmlService.createHtmlOutputFromFile("index.html");
  SpreadsheetApp.getUi().showModalDialog(html, 'sample');
}

index.html:HTML和Javascript

<script src="https://download.playfab.com/PlayFabClientApi.js"></script>
<script>
  function PlayFabAPICall() {
    PlayFab.settings.titleId = "E3FA";
    var loginRequest = {
      // Currently, you need to look up the correct format for this object in the API-docs:
      // https://api.playfab.com/documentation/Client/method/LoginWithCustomID
      TitleId: PlayFab.settings.titleId,
      AuthTicket: token,
      CreateAccount: false,
      KongregateId: kongID,
    };

    PlayFabClientSDK.LoginWithKongregate(loginRequest, LoginCallback);
  }

  var LoginCallback = function (result, error) {
    if (result !== null) {
      console.log("Congratulations, you made your first successful API call!");
    }
    else if (error !== null) {
      console.log("Something went wrong with your first API call.\n" +
      "Here's some debug information:\n" +
      PlayFab.GenerateErrorReport(error));
    }
  }

  PlayFabAPICall();
</script>

注意:

  • 在运行脚本之前,请先设置AuthTicket: token,KongregateId: kongID

参考: