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?