question

justin-4 avatar image
justin-4 asked

Azure function to LoginWithServerCustomId fails with bad request

I'm calling a custom Azure Function from Unity via UnityWebRequest that validates the player with Meta then logs in to PlayFab LoginWithServerCustomId API.

     [FunctionName("LogInViaOculus")]
     public static async Task<IActionResult> Run(
         [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
         ILogger log)
     {
         string reqBody = await req.ReadAsStringAsync();
         LoginArgs args = JsonConvert.DeserializeObject<LoginArgs>(reqBody);

         var client = new HttpClient();
    
         // Meta validation code redacted
    
         // bool createNew and string customId from form
    
     client.DefaultRequestHeaders.Add("X-SecretKey", "MYACTUALAPPSECRETCODEGOESHERE");
    
             var loginReqCont = new FormUrlEncodedContent(new[] {
                 new KeyValuePair&lt;string, string&gt;("CreateAccount", args.createNew.ToString()),
                 new KeyValuePair&lt;string, string&gt;("ServerCustomId", args.customId),
             });
    
             string loginUri = $"https://APPID.playfabapi.com/Server/LoginWithServerCustomId";
             HttpResponseMessage loginResponse = await client.PostAsync(
                 loginUri, loginReqCont);
    
             if (!loginResponse.IsSuccessStatusCode)
             {
                 log.LogError($"PlayFab Login Error {loginResponse.StatusCode}: {loginResponse.ReasonPhrase}");
                 return new BadRequestObjectResult($"PlayFab Login Error {loginResponse.StatusCode}: {loginResponse.ReasonPhrase}");
             }
    
             string loginResult = null;
             using (var reader = new StreamReader(await loginResponse.Content.ReadAsStreamAsync()))
             {
                 loginResult = await reader.ReadToEndAsync();
             }
    
             return new OkObjectResult(loginResult);

}

But this returns 400 Bad Request and no other data. Did I miss an authentication? Is there a wait to get more data out of this error?

apis
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

PlayFab API accepts data in JSON format as the request body, so you may use the StringContent class to create a request body containing JSON data. Here is the code example:

 var client = new HttpClient();
        
             client.DefaultRequestHeaders.Add("X-SecretKey", "SecretKey");
                 
              var requestBody = new JObject
             {
                 ["ServerCustomId"] = "servercustomIdTest",
                 ["CreateAccount"] = true
             };
        
              var loginReqCont = new StringContent(requestBody.ToString(),Encoding.UTF8,"application/json");
        
              string loginUri = $https://TitleId.playfabapi.com/Server/LoginWithServerCustomId;
                 
              HttpResponseMessage loginResponse = await client.PostAsync(loginUri, loginReqCont);
        
              if (!loginResponse.IsSuccessStatusCode)
              {
                  log.LogError($"PlayFab Login Error {loginResponse.StatusCode}: {loginResponse.ReasonPhrase}");
                  return new BadRequestObjectResult($"PlayFab Login Error {loginResponse.StatusCode}: {loginResponse.ReasonPhrase}");
              }
        
              string loginResult = null;
              using (var reader = new StreamReader(await loginResponse.Content.ReadAsStreamAsync()))
              {
                  loginResult = await reader.ReadToEndAsync();
              }
        
              return new OkObjectResult(loginResult);

In addition, you may run “dotnet add package PlayFabAllSDK” to install the package to your AzureFunction project and call Login With Server CustomId API directly instead of make http request manually.

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.

justin-4 avatar image justin-4 commented ·

Thanks, I already had PlayFabAllSDK installed, I just forgot about checking it. Going that route is much cleaner and got me up and running.

1 Like 1 ·
justin-4 avatar image
justin-4 answered

I should note that I had very similar JavaScript code working on the Unity Gaming Services Cloudscript, the only difference is that I don’t have the Bearer authentication, but that was provided via UGS as far as I know. Do I need to (and how do I) get/pass the token from the client?

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.