question

Fausto Bernardini avatar image
Fausto Bernardini asked

Error when running a Cloud Script Azure Function to reset Leaderboard via Scheduled Task

I am using the following Azure Cloud Script function to reset my leaderboard. I registered the function with trigger type HTTP and created a scheduled task to run it. When I "Save and Run" the task I get the following error: "Cloud script function Reset Daily Highest Score Leaderboard returned value that was too large"

As you can see from the code, I tried to return very little... what else could be wrong?

 using System.Threading.Tasks;
 using Microsoft.AspNetCore.Http;
 using Microsoft.Azure.WebJobs;
 using Microsoft.Azure.WebJobs.Extensions.Http;
 using Microsoft.Extensions.Logging;
 using Newtonsoft.Json;
 using PlayFab;
 using PlayFab.Samples;
    
 namespace NewYorkShowsOff.WordLace
 {
     public static class ResetDailyHighestScoreLeaderboard
     {
         [FunctionName("ResetDailyHighestScoreLeaderboard")]
         public static async Task<dynamic> Run(
             [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] 
             HttpRequest req, ILogger log)
         {
             ScheduledTaskFunctionExecutionContext<dynamic> context = 
                 JsonConvert.DeserializeObject<ScheduledTaskFunctionExecutionContext<dynamic>>(
                     await req.ReadAsStringAsync()
                 );
             //var args = context.FunctionArgument;
             //var lbName = args["leaderboard_name"];   // call with {"leaderboard_name" : "Daily Highest Score"}
     
             var newRequest = new PlayFab.AdminModels.IncrementPlayerStatisticVersionRequest 
                 { 
                     StatisticName = "Daily Highest Score" 
                 };
    
             var adminApi = new PlayFabAdminInstanceAPI(
                 new PlayFabApiSettings
                 {
                     TitleId = context.TitleAuthenticationContext.Id
                 },
                 new PlayFabAuthenticationContext
                 {
                     EntityToken = context.TitleAuthenticationContext.EntityToken
                 }
             ); 
    
             var newResult = await adminApi.IncrementPlayerStatisticVersionAsync(newRequest);
    
             log.LogInformation($">> ResetLB: {0}, v{1}, DeactivationTime: {2}", 
                 newResult.Result.StatisticVersion.StatisticName,
                 newResult.Result.StatisticVersion.Version,
                 newResult.Result.StatisticVersion.DeactivationTime
             );
     
             //return new { newResult.Result };
             //return new { newResult.Result.StatisticVersion };
             return new {};
         }
     }
 }
CloudScriptLeaderboards and Statisticsscheduled tasks
10 |1200

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

Xiao Zha avatar image
Xiao Zha answered

The Admin APIs require a SecretKey to call. You can refer to Where to set DeveloperSecretKey for Azure PlayFabServerAPI calls - Playfab Community to know how to set DeveloperSecretKey. And here is the code example you can refer to:

 public static class Function
     {
         [FunctionName("Test")]
         public static async Task&lt;dynamic&gt; Run(
              [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)]
              HttpRequest req, ILogger log)
         {
             ScheduledTaskFunctionExecutionContext&lt;dynamic&gt; context = JsonConvert.DeserializeObject&lt;ScheduledTaskFunctionExecutionContext&lt;dynamic&gt;&gt;( await req.ReadAsStringAsync() );
             var newRequest = new PlayFab.AdminModels.IncrementPlayerStatisticVersionRequest
             {
                 StatisticName = "Test"
             };
    
             var adminApi = new PlayFabAdminInstanceAPI(
                 new PlayFabApiSettings
                 {
                     TitleId = context.TitleAuthenticationContext.Id,
                     DeveloperSecretKey= Environment.GetEnvironmentVariable("PLAYFAB_DEV_SECRET_KEY", EnvironmentVariableTarget.Process)
                 },
                 new PlayFabAuthenticationContext
                 {
                     EntityToken = context.TitleAuthenticationContext.EntityToken
                 }  
             );
             var newResult = await adminApi.IncrementPlayerStatisticVersionAsync(newRequest);
             log.LogInformation($"&gt;&gt; ResetLB: {0}, v{1}, DeactivationTime: {2}",
                 newResult.Result.StatisticVersion.StatisticName,
                 newResult.Result.StatisticVersion.Version,
                 newResult.Result.StatisticVersion.DeactivationTime
             );
             return new { };
         }
     }
10 |1200

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

Fausto Bernardini avatar image
Fausto Bernardini answered

Thank you!

I set my PLAYFAB_DEV_SECRET_KEY in Azure App Settings and changed the code as you suggested, but I still get the same error. Any other thoughts?

10 |1200

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

Fausto Bernardini avatar image
Fausto Bernardini answered

Update, go the function to work!

I had the wrong URL in PlayFab Automation | Cloud Script | Register Function.

For the record, you need to get the URL from your Azure portal, under Functions | Get Function URL (seems obvious but I had overlooked it :). )

1 comment
10 |1200

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

Fausto Bernardini avatar image Fausto Bernardini commented ·

As an aside to Azure/PlayFab... the error message did not make this easy to debug :)

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.