eordoghdaniel avatar image
eordoghdaniel asked

GrantItemsToUser with Unity and Azure Functions can't make it work

I am trying for to figure out how to grant an item to the player from the client side using Azure Functions.

  1. I see on PlayStream that the function was triggered
  2. I see in Azure Portal the CloudScript was triggered
  3. 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?


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.");
            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
        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);

unity3dCloudScriptPlayer Inventory
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

Made Wang avatar image
Made Wang answered

I tested your code, and it works. Have you created a new key-value pair for PLAYFAB_DEV_SECRET_KEY in the Azure portal? Refer to Configure function app settings in Azure Functions | Microsoft Docs. You can also test it locally, refer to Local debugging for Cloudscript using Azure Functions - PlayFab | Microsoft Docs. Also, are you getting any error messages in the Log stream?

10 |1200

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

eordoghdaniel avatar image eordoghdaniel commented ·

Thank you for the reply @Made Wang! I have "PLAYFAB_DEV_SECRET_KEY" setup in Azure Portal as well as "PLAYFAB_TITLE_ID".

I get in Unity the successful response on the call:

I see in Azure Portal as well the Functions getting executed:

My problem is still in PlayFab the Player doesn't receive the Item that I wanted to grant. It is not in the players inventory. Do I miss some fields from the request like "CatalogVersion"? Is that mandatory?

I am a little bit lost, it's nice to see that the call works and I get the successful response, but ultimately the goal is to grant the item.

0 Likes 0 ·
eordoghdaniel avatar image eordoghdaniel commented ·

@Made Wang it's working now. I really appreciate your feedback it helped.

My issue was that I didn't add the CS2AFHelperClasses.cs to the Local build and redeploy the function app, but I simply added the CS2AFHelperClasses code to the GrantEquipmenToPlayer function.

As soon as I added to the Local version the supporting namespaces it started working!

0 Likes 0 ·
Made Wang avatar image Made Wang eordoghdaniel commented ·

Glad to know you solved this issue.

0 Likes 0 ·

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.