question

Darius Vu avatar image
Darius Vu asked

CloudScriptAzureFunctionsHTTPRequestError happens sometimes

Dear team,

I am using Azure Function to create Kid's accounts from parent's account and update data for kid's account after creating it. But it sometimes has the issue "CloudScriptAzureFunctionsHTTPRequestError" and sometimes it is working well. Do you know why and how to fix it?

Here is my code:

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

            //Get Input Data
            string username = args["Username"];
            string password = args["Password"];
            string displayName = args["DisplayName"];
            string profileData = args["ProfileData"];

            //Output Data
             UserData userData = new UserData();

            //set up title id and secret key for admin api to use 
            var adminApiSettings = new PlayFab.PlayFabApiSettings()
            {
                TitleId = "XXXXX",
                DeveloperSecretKey = "XXXXXXXXXX....",
            };
            
            var adminAPI = new PlayFab.PlayFabAdminInstanceAPI(adminApiSettings);
            var clientAPI = new PlayFab.PlayFabClientInstanceAPI(adminApiSettings);

            // Create Account using Username and Password
            var registerUserResponse = await clientAPI.RegisterPlayFabUserAsync( new PlayFab.ClientModels.RegisterPlayFabUserRequest()
            {
                Username = username,
                Password = password,
                RequireBothUsernameAndEmail = false
            });
       
            userData.PlayFabID = registerUserResponse.Result.PlayFabId;
            userData.Entity = ConvertClientToGroupnModels(registerUserResponse.Result.EntityToken.Entity);

            // Set DisplayName
            var updateDisplayNameResponse = await adminAPI.UpdateUserTitleDisplayNameAsync (new PlayFab.AdminModels.UpdateUserTitleDisplayNameRequest()
            {
                PlayFabId = userData.PlayFabID ,
                DisplayName = displayName
            });
            
            // Update User Data Title
            Dictionary<string, string> userDataTitle = new Dictionary<string, string>() {
                {"ProfileData", profileData}};

            await adminAPI.UpdateUserDataAsync (new PlayFab.AdminModels.UpdateUserDataRequest()
            {
                PlayFabId = userData.PlayFabID ,
                Data = userDataTitle,
                Permission = PlayFab.AdminModels.UserDataPermission.Public
            });

            return userData ;
        }

        public static PlayFab.GroupsModels.EntityKey ConvertClientToGroupnModels(PlayFab.ClientModels.EntityKey entityKey)
        {
            PlayFab.GroupsModels.EntityKey rtn = new PlayFab.GroupsModels.EntityKey();
            rtn.Id = entityKey.Id;
            rtn.Type = entityKey.Type;
            return rtn;
        }
    }

    public class UserData
    {
        public string PlayFabID = ""; 
        public PlayFab.GroupsModels.EntityKey Entity;
    }

Thank you so much for your help!!!

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

·
Seth Du avatar image
Seth Du answered

You may need to add debugging sentences in your code and review Azure function document on Monitor Azure Functions | Microsoft Docs to collect logs. Otherwise, Following the instructions on Debugging Azure Functions - PlayFab | Microsoft Docs to streaming logs is an alternative solution.

According to your code, you are using client API in Azure function. Because Cloud Script/ Azure Function is serverless, developers cannot interfere backend server management. The IP address of current-running instance can stay unchanged for a long time, while PlayFab client API has a certain upper bound rate limit per IP, which may cause throttling when the request is high. If you want to create account on server, please use LoginWithServerCustomId Server API, then pass through the session ticket/ entity token back to the client for parents to add other login identities.

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.