question

Kim Strasser avatar image
Kim Strasser asked

CloudScript: PlayFab.PlayFabException Must be logged in to call this method

I get this exception in my client code if I call RegisterPlayFabUser.

PlayFab.PlayFabException has been thrown

Must be logged in to call this method

In addition, the new account is not created because I don't get a notification in PlayStream when I try to create the player's account in CloudScript:

Why is the player not logged in if I call RegisterPlayFabUser in CloudScript? What is wrong with my code?

Client code:

private async Task RegisterPlayFabUserCloud()
{
    var result = await PlayFabClientAPI.ExecuteCloudScriptAsync(new ExecuteCloudScriptRequest()
    {
        FunctionName = "RegisterPlayFabUser",
        FunctionParameter = new { SessionTicket = playersessionTicket, Email = "player2test@gmail.com", Username = "Player2username", Password = "Player2password!!!" },
        GeneratePlayStreamEvent = true
    });
 
    if (result.Error != null)
        Console.WriteLine(result.Error.Error.ToString());
    else
    {
        if (result.Result.Logs.Count() > 0)
            Console.WriteLine(result.Result.Logs[0].Message);
    }
}

CloudScript:

handlers.RegisterPlayFabUser = function (args, context)
{
     var resultprofile = server.GetPlayerProfile(
       {
           PlayFabID: currentPlayerId
       });
       
     var result = RegisterPlayFabUserFromCloudScript(args.SessionTicket, args.Email, args.Username, args.Password, currentPlayerId);
 
      return result;
}
 
function RegisterPlayFabUserFromCloudScript(sessionticket, email, username, password, playFabId)
{
    var contentBodyTemp = {
        "TitleId": BFD0A,
        "RequireBothUsernameAndEmail": true,
        "PlayFabId": playFabId,
        "Email": email,
        "Username": username,
        "Password": password
    };
    
    let url = "https://BFD0A.playfabapi.com/Client/RegisterPlayFabUser";
    let method = "POST";
    let contentBody = `{"TitleId": "${BFD0A}", "RequireBothUsernameAndEmail": "${true}", "PlayFabId": "${playFabId}", "Email": "${email}", "Username": "${username}", "Password": "${password}"}`;
    let contentType = "application/json";
    let headers = { "X-Authentication": sessionticket };
    let responseString = http.request(url, method, contentBody, contentType, headers);
    let responseJSONObj = JSON.parse(responseString);
    return (responseJSONObj);
}
CloudScript
10 |1200

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

1 Answer

·
Sarah Zhang avatar image
Sarah Zhang answered

Because ExecuteCloudScript is a Client API, it can’t be called anonymously. That means we should login first before we called this API. So, actually, we can’t register PlayFab users with CloudScript that depends on ExecuteCloudScript to run its function. If you want to register users on the server-side, you can consider the custom server. Or you can register the user on the clients directly.

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

Kim Strasser avatar image Kim Strasser commented ·

Would it be possible to always first automatically login the player with LoginWithIOSDeviceID and right after that I would call AddUsernamePassword in my CloudScript to add username, password and email to the player's account?

I would call AddUsernamePassword in CloudScript like this: https://community.playfab.com/questions/34546/cloudscript-post-method-for.html?childToView=34561#answer-34561

Does it make a difference if I use the combination LoginWithIOSDeviceID+AddUsernamePassword or if I only use RegisterPlayFabUser instead of LoginWithIOSDeviceID+AddUsernamePassword?

Does it(LoginWithIOSDeviceID+AddUsernamePassword) work on all iOS and Android devices?

0 Likes 0 ·
Sarah Zhang avatar image Sarah Zhang Kim Strasser commented ·

@Kim Strasser

>>> Would it be possible to always first automatically login the player with LoginWithIOSDeviceID and right after that I would call AddUsernamePassword in my CloudScript to add username, password and email to the player's account?

Yes, it's possible.

>>> Does it make a difference if I use the combination LoginWithIOSDeviceID+AddUsernamePassword or if I only use RegisterPlayFabUser instead of LoginWithIOSDeviceID+AddUsernamePassword?

It doesn't make a material difference.

>>> Does it(LoginWithIOSDeviceID+AddUsernamePassword) work on all iOS and Android devices?

On Android devices, you can try LoginWithAndroidDeviceID.

0 Likes 0 ·

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.