question

michaelbb avatar image
michaelbb asked

C#/UWP: AddFriend per Cloud Script fails

Hi there,

I am trying to add a friend via cloud script. Unfortunately I get an error which i tried to solve, unsuccessfully :-(

I receive the following:

"Logs": [
            {
                "Level": "Error",
                "Message": "PlayFab API request error",
                "Data": {
                    "api": "/Server/AddFriend",
                    "request": {
                        "FriendsListPlayFabId": "E1C24F9E75861970",
                        "PlayFabId": "957122DB610D6A8E"
                    },
                    "result": null,
                    "apiError": {
                        "code": 400,
                        "status": "BadRequest",
                        "error": "InvalidParams",
                        "errorCode": 1000,
                        "errorMessage": "Invalid input parameters",
                        "errorHash": null,
                        "errorDetails": null
                    }
                }




And my cloud script looks like: 

handlers.AddFriend = function(args){
var friendlist={};
friendlist.FriendsListPlayFabId = currentPlayerId;
friendlist.PlayFabId = args.Idd;
server.AddFriend(friendlist);

}



In my C# code, I have the following:

private void BtnCloudFriend_Click(object sender, RoutedEventArgs e)
        {
            AddFriendCloud("957122DB610D6A8E");
        }
        public static async void AddFriendCloud(string FabId)
        {
            await StartCloudFriend("957122DB610D6A8E").ContinueWith(executeCloudScriptResult =>
        OnCloudHelloWorld(executeCloudScriptResult.Result.Result));
        }
        private static async Task<PlayFabResult<ExecuteCloudScriptResult>> StartCloudFriend(string a)
        {
            var executeCloudScriptRequest = new ExecuteCloudScriptRequest()
            {
                FunctionName = "AddFriend",
                FunctionParameter = new { Idd = a },
                GeneratePlayStreamEvent = true,
            };
            return await PlayFabClientAPI.ExecuteCloudScriptAsync(executeCloudScriptRequest);
        }




Well, as I am completely new to cloud script, I really cannot find out what's happening and why there is the error message. Any help would be appreciated.


Best wishes and many thanks in advance,

Michael

CloudScriptFriendswindows
10 |1200

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

Citrus Yan avatar image
Citrus Yan answered

Hi Michael,

Your C# code looks alright to me, however, there are some errors in Cloud Script:

1)There is no property called “FriendsListPlayFabId” in Server/AddFriend API, you might be referring to “FriendPlayFabId” instead.

2)“PlayFabId” property is the PlayFab identifier of the player to add a new friend, you need to pass “currentPlayerId” to it instead of passing “args.Idd”. “FriendPlayFabId” is the PlayFab identifier of the user being added, so you need to pass “args.Idd” to it.

The following Cloud Script code I tested should work:

handlers.AddFriend =function(args){

var friendlist={};

friendlist.FriendPlayFabId = args.Idd;

friendlist.PlayFabId = currentPlayerId;

server.AddFriend(friendlist);

}
10 |1200

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

michaelbb avatar image
michaelbb answered

Hi @Citrus Yan,

many thanks for the quick response.

It's working like you mentioned, but I was trying to do the opposite:

Player A adds Player B to his friendlist.

And via cloud script I tried to achieve that Player A will be added to Player B's friendlist as well.

How could that be accomplished?

What a pity, thought, I was on the right way :-(

Many thanks and best wishes,

michael

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.

Citrus Yan avatar image Citrus Yan commented ·

Are you saying that Player A will be added to Player B's friend list without confirmation from Player B? If that's the case, you can simply swap the parameter passed to PlayFabId and FriendPlayFabId after Player A added Player B. The following code should work:

handlers.AddFriend = function(args){
var friendlist={};
friendlist.FriendPlayFabId = args.Idd;
friendlist.PlayFabId = currentPlayerId;
server.AddFriend(friendlist);
friendlist.PlayFabId = args.Idd;
friendlist.FriendPlayFabId = currentPlayerId;
server.AddFriend(friendlist);
}

Moreover, if what you really want is a two-way friend confirmation with Cloud Script, you might need to refer to this thread for help: https://community.playfab.com/questions/370/206712537-Two-way-friend-confirmation-with-Cloud-Script.html

0 Likes 0 ·
michaelbb avatar image
michaelbb answered

Hi @Citrus Yan,

many thanks for your quick and helpful reply.

Yes, you're right, that definitely makes sense.

I was trying to expand my initial step by setting friend tags.

handlers.AddFriend = function(args){
var tagRequest={};
tagRequest.Tags = ["Invitation"];
tagRequest.PlayFabId = args.Idd;
tagRequest.FriendPlayFabId = currentPlayerId;
var res = server.SetFriendTagsRequest(tagRequest);
tagRequest.Namespace = "Sent"
tagRequest.PlayFabId = currentPlayerId;
tagRequest.FriendPlayFabId = args.Idd;
server.SetFriendTagsRequest(tagRequest);

var friendlist={};
friendlist.FriendPlayFabId = args.Idd;
friendlist.PlayFabId = currentPlayerId;
server.AddFriend(friendlist);
friendlist.PlayFabId = args.Idd;
friendlist.FriendPlayFabId = currentPlayerId;
server.AddFriend(friendlist);

}

But I received the following error message that "server.SetFriendTagsRequest is not a function of AddFriend.

I thought, it's possible to combine two cloud script executions or is there any mistake in my cloud script?

Many thanks in advance and best wishes,

Michael

10 |1200

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

michaelbb avatar image
michaelbb answered

Hi @Citrus Yan,

many thanks. I've found my mistake :-)

Best wishes,

Michael

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.

Citrus Yan avatar image Citrus Yan commented ·

Glad to hear that:)

0 Likes 0 ·
mildgravy0 avatar image
mildgravy0 answered

Hi @michaelbb
Can you tell me please what was the mistake?

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.