question

David Dick avatar image
David Dick asked

How Can I Use PlayFab.PlayFabServerAPI.AddFriendAsync In An Azure Function?

Hi there, I am trying to write a simple Cloud Script Function that uses the Playfab ServerAPI through an Azure Function to add a friend which is written in C#. I am just trying to understand certain concepts at the moment and that is why I am not using the ClientAPI through Unity but rather the ServerAPI in conjunction with Azure Functions. I have gone through the "Hello World" example and can successfully call a cloud script and return a result in Unity.

I don't have an understanding of PlayFab.PlayFabServerAPI.AddFriendAsync method and would like some clarification as to what "Data" needs to be passed into the call and if I am even close to a proper approach. Any information is greatly appreciated!

The error I am getting in Unity is {"result":{},"customData":null,"error":null}

Here is the code I have written as an Azure Function to try to add a friend via a Cloud Script:

namespace NetworkSystem.FriendRequest
{
    public static class FriendRequest
    {
        [FunctionName("FriendRequest")]
        public static async Task<dynamic> Run(
        [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] 
	HttpRequest req, ILogger log)
        {
            var context = JsonConvert.DeserializeObject
	    <FunctionExecutionContext<dynamic>>(await req.ReadAsStringAsync());

            dynamic args = context.FunctionArgument;
            string friendEmail = string.Empty;

            if (args != null && args["friendEmail"] != null)
            {
                friendEmail = args["friendEmail"];
            } 

            var apiSettings = new PlayFabApiSettings   
            {  
                TitleId = 
		Environment.GetEnvironmentVariable("PLAYFAB_TITLE_ID", 
		EnvironmentVariableTarget.Process), 
                DeveloperSecretKey = 
		Environment.GetEnvironmentVariable("PLAYFAB_DEV_SECRET_KEY", 
		EnvironmentVariableTarget.Process),  
            };   
 
            var serverApi = new PlayFabServerInstanceAPI(apiSettings);

            var request = new PlayFab.ServerModels.AddFriendRequest
            {
                PlayFabId = context.CallerEntityProfile.Lineage.TitlePlayerAccountId,
                FriendEmail = friendEmail
            };

            var result = await serverApi.AddFriendAsync(request);

            return result;
        }
    }
}

apisCloudScriptdata
10 |1200

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

David Dick avatar image
David Dick answered

I have edited the code and got the ServerAPI.AddFriendAsync to work within the Azure Function. This does raise a few more questions that may need to be addressed on a new thread, however, I am now curious as to why friends are only added using the MasterPlayerAccountID and if this means friends will need to be added only once for multiple titles?

Here is the revised code:

namespace NetworkSystem.FriendRequest
{
    public static class FriendRequest
    {
        [FunctionName("FriendRequest")]
        public static async Task<dynamic> Run(
        [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] 
        HttpRequest req, ILogger log)
        {
            var context = JsonConvert.DeserializeObject
	    <FunctionExecutionContext<dynamic>>(await req.ReadAsStringAsync());

            dynamic args = context.FunctionArgument;
            string friendEmail = args["friendEmail"];

            PlayFabSettings.staticSettings.DeveloperSecretKey = 
            Environment.GetEnvironmentVariable("PLAYFAB_DEV_SECRET_KEY", 
            EnvironmentVariableTarget.Process);
            PlayFabSettings.staticSettings.TitleId = 
            Environment.GetEnvironmentVariable("PLAYFAB_TITLE_ID", 
            EnvironmentVariableTarget.Process);

            var request = new PlayFab.ServerModels.AddFriendRequest
            {
                PlayFabId = context.CallerEntityProfile.Lineage.MasterPlayerAccountId,
                FriendEmail = friendEmail     
            };

            return await PlayFabServerAPI.AddFriendAsync(request);
        }
    }
}
10 |1200

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

Adrian Taylor avatar image
Adrian Taylor answered

I'm trying to do something similar to what you have here, but I can't find where the PlayfabServerAPI lives... what namespace is it under?

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.