question

joon avatar image
joon asked

calling PlayFabCloudScriptAPI.ExecuteFunction with Admin or secret key

I'm trying to call a Azure Functions cloudscript function through the Unity Playfab SDK.

It seems to me at the moment this is only possible with a Client Entity token, and not within the Admin context. My code works when I am in Play mode and the client has logged in.

PlayFabCloudScriptAPI.ExecuteFunction(new ExecuteFunctionRequest()
{
    Entity = new PlayFab.CloudScriptModels.EntityKey()
    {
        Id = PlayFabSettings.staticPlayer.EntityId, 
        Type = PlayFabSettings.staticPlayer.EntityType,
    },
    FunctionName = "myFunction",
    FunctionParameter = new Dictionary<string, object>()
    {
        {"player", playfabID},
    }
}, (ExecuteFunctionResult result) => { Debug.Log(result); 
}, (PlayFabError error) => { Debug.Log(error.GenerateErrorReport()); 
});



But I need it to work in Admin context, in editor-mode, for maintenance functions.

This line from PlayFabCloudScriptAPI.cs prevents the function from being called

if (!context.IsEntityLoggedIn()) 

throw new PlayFabException(PlayFabExceptionCode.NotLoggedIn,"Must be logged in to call this method");

If I comment it out, it tries to execute it but I get the following error:

This API method does not allow anonymous callers.

PS: I've asked a similar question but I can't seem to edit it, or remove it. Sorry for double posting.

https://community.playfab.com/questions/56806/call-cloudscript-function-from-unity-editor.html?childToView=56842#comment-56842

apissdks
10 |1200

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

Gosen Gao avatar image
Gosen Gao answered

This error is caused by missing EntityToken. As a workaround, you can call it in the resultCallback of AuthenticationAPI.GetEntityToken. This API will login your title with SecretKey and return a title level EntityToken.

10 |1200

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

joon avatar image
joon answered

public static async void CallCloudScriptEditorTime(string somePlayfabID) { var isDone = false; var entityToken = ""; PlayFab.AuthenticationModels.EntityKey entity = null; PlayFabAuthenticationAPI.GetEntityToken( new PlayFab.AuthenticationModels.GetEntityTokenRequest(){}, (result)=>{ isDone = true; Debug.Log(result.EntityToken); entityToken = result.EntityToken; entity = result.Entity; }, (error)=>{ isDone = true; Debug.Log(error.GenerateErrorReport()); } ); while (!isDone) { await Task.Delay(1000); } PlayFabCloudScriptAPI.ExecuteFunction(new ExecuteFunctionRequest() { AuthenticationContext = new PlayFabAuthenticationContext("",entityToken,somePlayfabID,entity.Id, entity.Type), Entity = new PlayFab.CloudScriptModels.EntityKey() { Id = entity.Id, Type = entity.Type }, FunctionName = "MyFunction", }, (result) => { // Do something with result }, (error) => { // Error }); }

Thanks for the tip, I got it to work with the above code.

10 |1200

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

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.