我正在Google电子表格中创建一个新的小项目,从Playfab服务器获取一些数据,用于孔雷盖特的游戏。Playfab提供了一个Javascript API来工作:
https://download.playfab.com/PlayFabClientApi.js
我会使用这个函数
但是,当我尝试运行第一个测试时,我会得到错误消息:
ReferenceError: "Promise" no está definido. (línea 33, archivo "Código")经过一些研究,我发现GAS不支持这个承诺(Google脚本),但是我在某个地方读到了V8可以使用的承诺.我有点迷路了,你能帮我把这件事做好吗?
我项目中的代码:
// 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);});
},发布于 2019-04-22 22:58:57
PlayFabAPICall()运行https://download.playfab.com/PlayFabClientApi.js脚本。如果我的理解是正确的,那么这个示例脚本如何?不幸的是,在目前阶段,“承诺”还不能在服务器端的Google脚本中使用。因此,作为当前的解决办法,我建议使用自定义对话框和侧栏。在这个示例脚本中,使用了一个自定义对话框。此脚本的流程如下所示。请把这看作是几个答案中的一个。
openDialog()脚本。index.html就会运行。index.html在浏览器上运行。
示例脚本:
请将以下脚本复制并粘贴到脚本编辑器中。Code.gs和index.html分别是脚本和html。
Code.gs: Google脚本
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。参考文献:
https://stackoverflow.com/questions/55788388
复制相似问题