首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Playfab - ReferenceError:“承诺”未定义

Playfab - ReferenceError:“承诺”未定义
EN

Stack Overflow用户
提问于 2019-04-22 02:10:20
回答 1查看 361关注 0票数 1

我正在Google电子表格中创建一个新的小项目,从Playfab服务器获取一些数据,用于孔雷盖特的游戏。Playfab提供了一个Javascript API来工作:

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

我会使用这个函数

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

代码语言:javascript
复制
ReferenceError: "Promise" no está definido. (línea 33, archivo "Código")

经过一些研究,我发现GAS不支持这个承诺(Google脚本),但是我在某个地方读到了V8可以使用的承诺.我有点迷路了,你能帮我把这件事做好吗?

我项目中的代码:

代码语言:javascript
复制
// 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:

代码语言:javascript
复制
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);});
},
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-22 22:58:57

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

如果我的理解是正确的,那么这个示例脚本如何?不幸的是,在目前阶段,“承诺”还不能在服务器端的Google脚本中使用。因此,作为当前的解决办法,我建议使用自定义对话框和侧栏。在这个示例脚本中,使用了一个自定义对话框。此脚本的流程如下所示。请把这看作是几个答案中的一个。

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

示例脚本:

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

Code.gs: Google脚本

代码语言:javascript
复制
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

代码语言: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

参考文献:

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55788388

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档