question

Kim Strasser avatar image
Kim Strasser asked

Is it possible to call an Azure Function in CloudScript?

I check the players username, password and email in cloud script before I call function AddUsernamePasswordFromCloudScript because not all characters are supported in my game. Would it be possible to replace the cloud script function AddUsernamePasswordFromCloudScript with an Azure function that does exactly the same thing?

How can I call an Azure Function in CloudScript? What API can I use?

Cloud script function that I want to replace:

function AddUsernamePasswordFromCloudScript(sessionticket, email, username, password, playFabId)
{
    var contentBodyTemp = {
        "PlayFabId": playFabId,
        "Email": email,
        "Username": username,
        "Password": password
    };
    
    let url = "https://E5E2C.playfabapi.com/Client/AddUsernamePassword";
    let method = "POST";
    let contentBody = `{"PlayFabId": "${playFabId}", "Email": "${email}", "Username": "${username}", "Password": "${password}"}`;
    let contentType = "application/json";
    let headers = { "X-Authentication": sessionticket };
    let responseString = http.request(url, method, contentBody, contentType, headers);
    let responseJSONObj = JSON.parse(responseString);
    return (responseJSONObj);
    
    if (responseJSONObj.Error == null)
        return "true";
    else
    {
        log.info("Error code: " + responseJSONObj.error.toString());
        return responseJSONObj.error;
    }
}
CloudScript
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

Yes, please refer to Sarah's answer in your previous thread: https://community.playfab.com/questions/45136/is-it-possible-to-use-addorupdatecontactemail-in-a.html.

ClientInstanceAPI will do the job. However, Client API may be throttled when there are frequent calls in the same IP address. You may also consider why it is necessary to implement client APIs in Cloud Script/Azure Function because the design purpose of client API is with limited permission for players to call in clients.

4 comments
10 |1200

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

Kim Strasser avatar image Kim Strasser commented ·

It's not working:

handlers.UpdateDisplaynameFromAzureFunction = function (args, context)
{
       var NewDisplayname = args.DesiredDisplayname;
       var DisplaynameChanged = false;
       // check here if the display name can be used.

       var result = server.ExecuteFunction(
       {
            Entity: {
                 Id: args.entityid,
                 Type: args.entitytype,
            },
            FunctionName: "ChangeDisplayname", // name of my Azure function
            FunctionParameter: { NewDisplayname: NewDisplayname }
       });
       
       if (result.Error == null)
       {
           log.info("True");
           DisplaynameChanged = true;
       }
       else
       {
           log.info("False");
           DisplaynameChanged = resultprofile.error;
       }
       
       return DisplaynameChanged;
}

Error:

 "Error": {
      "Error": "JavascriptException",
      "Message": "JavascriptException",
      "StackTrace": "TypeError: server.ExecuteFunction is not a function\n    at handlers.UpdateDisplaynameFromAzureFunction (E5E2C-main.js:2936:28)\n    at Object.invokeFunction (Script:116:33)"
    },

How can I call my Azure function in Cloud Script?

0 Likes 0 ·
Seth Du avatar image Seth Du ♦ Kim Strasser commented ·

Based on our test, it seems ExecuteFunction cannot be executed by Cloud Script. Usually, the API is included in entity API set, and in the normal circumstance, entity.xxxx should work. Meanwhile, after the tests, I reproduced a similar error like yours.

In a word, it is not supported, unless you craft an HTTP request in the cloud script then post it.

Your scenario seems odd to me, would you tell us what feature are you trying to implement and what is your concerns about those API calls in client?

0 Likes 0 ·
Kim Strasser avatar image Kim Strasser Seth Du ♦ commented ·

I need to use API Client/AddUsernamePassword and Admin/UpdateUserTitleDisplayName in Cloud Script or Azure Functions because I need to check the players desired username, password, email and display name first before I can make those two API calls. The player can only use these two API calls once, because it is not possible to change the display name in my game.

What are the best practices in this case?

Should I do everything in Cloud Script or everything Azure Functions?

Or would it be possible to check the players desired username, password, email and display name in Cloud Script and after that I would call with an HTTP request in Cloud Script my Azure function that executes the two APIs Client/AddUsernamePassword and Admin/UpdateUserTitleDisplayName?

Does it matter if I call Client/AddUsernamePassword and Admin/UpdateUserTitleDisplayName in Cloud Script or Azure Functions?

0 Likes 0 ·
Show more comments

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.