I am trying for to figure out how to grant an item to the player from the client side using Azure Functions.
- I see on PlayStream that the function was triggered
- I see in Azure Portal the CloudScript was triggered
- I have the CloudScript function set up in PlayFab
Still the item that I try to grant doesn't go through for the player. Where do I make the mistake?
Unity:
public void CallCSharpExecuteFunction() { PlayFabCloudScriptAPI.ExecuteFunction(new ExecuteFunctionRequest() { Entity = new PlayFab.CloudScriptModels.EntityKey() { Id = PlayFabSettings.staticPlayer.EntityId, //Get this from when you logged in, Type = PlayFabSettings.staticPlayer.EntityType, //Get this from when you logged in }, FunctionName = "GrantEquipmentToPlayer", //This should be the name of your Azure Function that you created. FunctionParameter = new Dictionary<string, object>() { { "id", "O1B5" } }, //This is the data that you would want to pass into your function. GeneratePlayStreamEvent = true //Set this to true if you would like this call to show up in PlayStream }, (ExecuteFunctionResult result) => { if (result.FunctionResultTooLarge ?? false) { Debug.Log("This can happen if you exceed the limit that can be returned from an Azure Function, See PlayFab Limits Page for details."); return; } Debug.Log($"The {result.FunctionName} function took {result.ExecutionTimeMilliseconds} to complete"); Debug.Log($"Result: {result.FunctionResult.ToString()}"); }, (PlayFabError error) => { Debug.Log($"Opps Something went wrong: {error.GenerateErrorReport()}"); }); }
Cloud script:
public static class GrantEquipmentToPlayer { [FunctionName("GrantEquipmentToPlayer")] public static async Task<dynamic> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log) { FunctionExecutionContext<dynamic> context = JsonConvert.DeserializeObject<FunctionExecutionContext<dynamic>>(await req.ReadAsStringAsync()); dynamic args = context.FunctionArgument; string itemId = args["id"]; List<string> itemIds = new List<string> { itemId }; Dictionary<string, string> customData = new Dictionary<string, string> { }; PlayFabSettings.staticSettings.DeveloperSecretKey = Environment.GetEnvironmentVariable("PLAYFAB_DEV_SECRET_KEY", EnvironmentVariableTarget.Process); PlayFabSettings.staticSettings.TitleId = context.TitleAuthenticationContext.Id; var authContext = new PlayFabAuthenticationContext { EntityToken = context.TitleAuthenticationContext.EntityToken }; var request = new GrantItemsToUserRequest { PlayFabId = context.CallerEntityProfile.Lineage.MasterPlayerAccountId, AuthenticationContext = authContext, ItemIds = itemIds }; return await PlayFabServerAPI.GrantItemsToUserAsync(request, customData); }