question

mwillows avatar image
mwillows asked

Update Player Currency in Azure functions

I've just started converting some existing cloudscript to azure functions. As a test I'm starting with a simple function that we currently use to add a daily bonus key right now during development( instead of waiting for 24 hrs), works great currently:

handlers.AddDailyBonus = function(args, context) {  server.AddUserVirtualCurrency({ "Amount": 1, "PlayFabId" : currentPlayerId, "VirtualCurrency": "DB"}); }

I started with the basic helloworld tutorial and uploaded it to my azure instance, and that operates correctly, so I know the basics of connecting to the azure functions is working. Following various tutorials and sample code I've arrived at the following to duplicate the above cloudscript:

[FunctionName("AddDailyBonus")]
        public static async Task<dynamic> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            FunctionExecutionContext<dynamic> context = JsonConvert.DeserializeObject<FunctionExecutionContext<dynamic>>(await req.ReadAsStringAsync());

            dynamic args = context.FunctionArgument;
/*
            var message = $"Hello {context.CallerEntityProfile.Lineage.MasterPlayerAccountId}!";
            log.LogInformation(message);

            dynamic inputValue = null;
            if (args != null && args["inputValue"] != null)
            {
                inputValue = args["inputValue"];
            }

            log.LogDebug($"HelloWorld: {new { input = inputValue} }");
*/
 
       var apiSettings = new PlayFab.PlayFabApiSettings () {
            TitleId = context.TitleAuthenticationContext.Id
        };

        PlayFab.PlayFabAuthenticationContext titlecontext = new PlayFab.PlayFabAuthenticationContext ();
            titlecontext.EntityToken = context.TitleAuthenticationContext.EntityToken;

       var request = new PlayFab.ServerModels.AddUserVirtualCurrencyRequest();
            request.Amount  = 1;
            request.PlayFabId = context.CallerEntityProfile.Lineage.MasterPlayerAccountId;
            request.VirtualCurrency = "DB";
    
      var serverApi = new PlayFab.PlayFabServerInstanceAPI(apiSettings, titlecontext); 
      
       var result = await serverApi.AddUserVirtualCurrencyAsync(request);

       return result; //new { messageValue = message };  

        }
    }

you can see where I've commented out the original helloworld function body. I've also switched from anonymous to function, and removed the "get" path.

When I attempt to call this from cloudscript, I get a 400 with 1473 error code, and I've tried a variety of options. As far as I can tell my other azure settings are correct, i.e. the title ID and dev key are in the host json and I can see them in my azure portal.

Any help would be greatly appreciated, feels like I'm missing something basic. Also, the various examples and the sample at https://github.com/PlayFab/PlayFab-Samples/blob/master/Samples/CSharp/AzureFunctions/ExampleFunctions/Handlers.cs vary enough that I'm not sure which is correct. I could not get the handlers.cs version to work correctly, probably something basic in my project settings.

apisCloudScript
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

·
JayZuo avatar image
JayZuo answered

After checking your code, I'd believe the problem here is that you forgot to set "DeveloperSecretKey" in your code. AddUserVirtualCurrency is a Server API requires a title secret key for authentication. And in C# SDK, we can provide title secret key by setting "DeveloperSecretKey" like the following:

          var apiSettings = new PlayFabApiSettings 
          { 
              TitleId =
context.TitleAuthenticationContext.Id, 
              DeveloperSecretKey =
Environment.GetEnvironmentVariable("PLAYFAB_DEV_SECRET_KEY",
EnvironmentVariableTarget.Process), 
          }; 
          var serverApi = new
PlayFabServerInstanceAPI(apiSettings);

As you've set title Id and dev key in Azure portal, you should be able to get dev key with "GetEnvironmentVariable" method, but you may need to change the key to match your setting. After this, there should be no issue with your Azure Function.

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.