question

Kim Strasser avatar image
Kim Strasser asked

Calling Azure function directly from the client fails, but with PlayFabCloudScriptAPI.ExecuteFunction it succeeds

I want to call this Azure function directly from the client without logging in and without using PlayFabCloudScriptAPI.ExecuteFunction. But it always fails.

On the other hand, calling the Azure function with PlayFabCloudScriptAPI.ExecuteFunction always succeeds.

namespace My.Functions
{
    public static class NewFunction
    {
        [FunctionName("ChangeDisplayname")]
        public static async Task<dynamic> MakeApiCall(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestMessage req, ILogger log)
        {
            var context = await FunctionContext<dynamic>.Create(req);
            var args = context.FunctionArgument;

            var desireddisplayname = args["NewDisplayname"];
			
            var request = new UpdateUserTitleDisplayNameRequest();
            request.PlayFabId = context.CallerEntityProfile.Lineage.MasterPlayerAccountId;
            request.DisplayName = desireddisplayname;

            var adminApi = new PlayFabAdminInstanceAPI(context.ApiSettings, context.AuthenticationContext);

            return await adminApi.UpdateUserTitleDisplayNameAsync(request);
        }
   }
}

In the client, I use the following code to call the Azure function without using PlayFabCloudScriptAPI.ExecuteFunction, but I always get this error:

Client code:

string url = "https://myplayfabfunctionapp.azurewebsites.net/api/ChangeDisplayname";          
string desireddisplayname = "Katie";
var response = string.Empty;

var parameters = new Dictionary<string, string>
{
    {"NewDisplayname", desireddisplayname}
};

string strPayload = JsonConvert.SerializeObject(parameters);
HttpContent c = new StringContent(strPayload, Encoding.UTF8, "application/json");
Uri uri = new Uri(url);

try
{
    using (var client = new HttpClient())
    {
        HttpRequestMessage request = new HttpRequestMessage
        {
            Method = HttpMethod.Post,
            RequestUri = uri,
            Content = c
        };

        HttpResponseMessage result = await client.SendAsync(request).ConfigureAwait(false);
        if (result.IsSuccessStatusCode)
        {
            response = await result.Content.ReadAsStringAsync().ConfigureAwait(false);
        }
    }
}
catch (Exception)
{
    //Handle Exception
}

What is wrong? How can I call the Azure function in the client without logging in and without using PlayFabCloudScriptAPI.ExecuteFunction?

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

·
Sarah Zhang avatar image
Sarah Zhang answered

For the clarification, if you don’t use PlayFab “ExecuteFunction” API, you couldn’t get the player’s context in the Azure Functions.

Generally, the player’s context is returned by login API and PlayFab “ExecuteFunction” API will help you pass it to the Azure Function. So if you don’t log the player in and use the “ExecuteFunction” API, the context won’t be passed to the function, the Azure Function you provided won’t work.

To summarize, you can refer to the following function to get the “NewDisplayname” field that passed by your custom “POST” method. But we don’t suggest to call the UpdateUserTitleDisplayName API without verifying the player’s authorization.

 [FunctionName("GetNameFromClient")]
        public static async Task<dynamic> GetNameFromClient([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequestMessage req, ILogger log)
        {
            string jsonStr = await req.Content.ReadAsStringAsync();
            Dictionary<string, string> jsonDict = JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonStr);
            string name = "";
            if (jsonDict.TryGetValue("NewDisplayname", out name))
            {
                log.LogInformation(name);
            }
            return new OkObjectResult("");
        }
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.