question

nilshartwig96 avatar image
nilshartwig96 asked

Retrieve Contact Email

Hey im trying to simply get the Contact Email with Azure Functions. In the Azure streaming logs it says "Object reference not set to an instance of an Object". What am i doing wrong?

This is my Azure Function:

[FunctionName("DisplayEmail")]
        public static async Task<IActionResult> 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;

            var apiSettings = new PlayFabApiSettings () 
            {
                TitleId = context.TitleAuthenticationContext.Id,
                DeveloperSecretKey = Environment.GetEnvironmentVariable("PLAYFAB_DEV_SECRET_KEY", EnvironmentVariableTarget.Process)
            };
 
            PlayFabAuthenticationContext titlecontext = new PlayFab.PlayFabAuthenticationContext ();
            titlecontext.EntityToken = context.TitleAuthenticationContext.EntityToken;
            
            var serverApi = new PlayFabServerInstanceAPI(apiSettings, titlecontext);

            

            GetPlayerProfileRequest emailRequest = new GetPlayerProfileRequest{
                PlayFabId = context.CallerEntityProfile.Lineage.MasterPlayerAccountId
            };

            PlayFabResult<GetPlayerProfileResult> emailResult = await serverApi.GetPlayerProfileAsync(emailRequest);

            var email = emailResult.Result.PlayerProfile.ContactEmailAddresses[0].EmailAddress;
            

            return new OkObjectResult(email);
        }

3 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.

Rick Chen avatar image Rick Chen ♦ commented ·

Your code looks fine. Which line did your Azure streaming log mention that the error occurs?

0 Likes 0 ·
nilshartwig96 avatar image nilshartwig96 Rick Chen ♦ commented ·

It mentions no line.

That's what it says:

2022-07-20T16:00:35.050 [Error] Executed 'DisplayEmail' (Failed, Duration=113ms)Object reference not set to an instance of an object.


0 Likes 0 ·
nilshartwig96 avatar image nilshartwig96 Rick Chen ♦ commented ·

And getting the Display Name, with basically the same Function works.

0 Likes 0 ·

1 Answer

·
nilshartwig96 avatar image
nilshartwig96 answered

I had to set the Profile Constraints in the Request. Now it works.

GetPlayerProfileRequest emailRequest = new GetPlayerProfileRequest{
                PlayFabId = context.CallerEntityProfile.Lineage.MasterPlayerAccountId,
                ProfileConstraints = new PlayerProfileViewConstraints{
                    ShowContactEmailAddresses = true
                }
            };

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.