question

shinichiglobalgaming avatar image
shinichiglobalgaming asked

How to call cloudscript function from javascript?

I have been trying to figure out for hours on how to call a very simple function in cloudscript from javascript. I've been searching for hours on the net but no luck so I have no choice but to ask here. I'm trying to make a game via html5 javascript. And I want to make a function call in cloudscript using the ExecuteCloudScript.

here is my javascript code for calling the cloud script function:

var helloworld = {};

helloworld.FunctionName = 'helloWorld';

PlayFabClientSDK.ExecuteCloudScript(helloworld, OnCloudHelloWorld, OnErrorShared);

var OnCloudHelloWorld = function(result)

{ console.log(result.data); }

var OnErrorShared = function(error){ console.log(error); }

I have already log in. Log.in is fine but when I execute the call for the cloud script. it says session ticket is null but I have already tried to do PlayFab.settings.sessionTicket = response.data.SessionTicket; but still not working

CloudScript
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
brendan avatar image
brendan answered

That error message would mean that your session ticket is not being passed to us in the required X-Authentication header of the call to ExecuteCloudScript. Are you using our SDK (https://api.playfab.com/sdks/javascript)? Can you provide the details of how you're logging in and how you're processing the result of the login call?

4 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

shinichiglobalgaming avatar image shinichiglobalgaming commented ·

How to pass a session ticket to playFab? I tried...

var LoginWithCustomIdRequest = {};

LoginWithCustomIdRequest.TitleId = id;

LoginWithCustomIdRequest.CustomId = cid;

LoginWithCustomIdRequest.CreateAccount = false;

OutputStatus("Logging into PlayFab...");

PlayFabClientSDK.LoginWithCustomID(LoginWithCustomIdRequest, (response, error) => { if(error) { OutputError(error); }

else {

// display account details

var result = response.data;

st = result.SessionTicket;

var status = "Login Successful. <br \\> Welcome Player: " + result.PlayFabId + "<br \\> Your session ticket is: " + result.SessionTicket;

OutputStatus(status); }

});


PlayFab.settings.developerSecretKey ="dsk";

PlayFab.settings.sessionTicket = st;

var helloworld = {};

helloworld.FunctionName = 'helloWorld';

PlayFabClientSDK.ExecuteCloudScript(helloworld, OnCloudHelloWorld, OnErrorShared);

var OnCloudHelloWorld = function(result)

{ console.log(result.data); }

var OnErrorShared = function(error){ console.log(error); }

My cloudscript function is quite simple... no parameters and returns a simple message coz I just want to try it out. Since I'm new I kinda like to test things out so I can explore much better

0 Likes 0 ·
brendan avatar image brendan shinichiglobalgaming commented ·

Well first, the Secret Key should never be in your client code (though I realize that's not actually your secret key - just being clear), as that would give a hacker the ability to change your title's configuration, delete players, etc.

Now, the Session Ticket is automatically saved in PlayFab._internalSettings when there is a successful login, if you're using our SDK. Once that happens, any Client API call would use the Session Ticket that was received in the response to the login. So if you're getting an error that the Session Ticket is invalid or missing, I'd have to guess that one of three things is the cause:

1. Not using the PlayFab SDK (can you confirm whether or not you are?)

2. The login was not successful (what is the output of your Login Successful log output?)

3. The Session Ticket has since been lost - either by a subsequent login call that failed, or overwriting it in _internalSettings.

If you have a repro that shows something different, can you send us the full project so that we can test it here?

1 Like 1 ·
shinichiglobalgaming avatar image shinichiglobalgaming brendan commented ·

I figured it out thanks to you, the problem was I was calling the loginrequest and the cloudscriptrequest at the same time... the login would take a few seconds to respond thus executing the cloudscript call without the sessionTicket. Anyway thanks it's just so hard to figure it out for the first time without a proper tutorial or anything. All I saw was unity tutorials. There was a javascript tutorial but it's only about the login which Im currently using.

Anyway my problem now is that since I'm using html5 javascript codes with no php I have no way of keepsafing my titleID or the customID for the player is that okay? I mean is it okay for those info to be seen unlike the Secret Key?

0 Likes 0 ·
Show more comments

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.