gpt4 book ai didi

javascript - Playfab - 引用错误 : "Promise" is not defined

转载 作者:行者123 更新时间:2023-11-30 19:37:27 26 4
gpt4 key购买 nike

我正在 Google 电子表格中创建一个新的小项目,以从 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 脚本)不支持 Promise,但是我在某个地方读到 V8 可以使用 promises...我有点迷茫,你能帮我做一点吗?

我的项目中的代码:

// 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);});
},

最佳答案

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

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

  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

引用资料:

关于javascript - Playfab - 引用错误 : "Promise" is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55788388/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com