question

andrewrasch avatar image
andrewrasch asked

Grant Items from Azure on Economy V2

Trying to grant users items from an Azure function but running into issues. I'm using the AddInventoryItems endpoint and seem to run into issues with authentication.

I've provided Title entity token for authentication, but setting the entitykey to the master_player_account of the user I want to modify the inventory for. I have disabled the policy to allow the user to add items to their own inventory for security reasons. Is there anyway to perform this operation without opening the permissions?

apisCloudScriptIn-Game Economy
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

·
Xiao Zha avatar image
Xiao Zha answered

The EntityKey in the AddInventoryItems API request should be title_player_account not the master_player_account. And since you use title entity token for authentication, player permission in the Economy Settings page won’t have an effect. Also, here is the code example you can refer to:

 public static class TestFunction1
     {
         [FunctionName("Itemtest")]
          public static async Task Run(
              [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
              ILogger log)
          {
              FunctionExecutionContext<dynamic> context = JsonConvert.DeserializeObject<FunctionExecutionContext<dynamic>>(await req.ReadAsStringAsync());
        
              var apiSettings = new PlayFabApiSettings
              {
                  TitleId = Environment.GetEnvironmentVariable("PLAYFAB_TITLE_ID", EnvironmentVariableTarget.Process),
                  DeveloperSecretKey = Environment.GetEnvironmentVariable("PLAYFAB_DEV_SECRET_KEY", EnvironmentVariableTarget.Process)
              };
                  
                  
              var titleAuthContext = new PlayFabAuthenticationContext();
              //set the authentication Token to Title Token and no need to set the EntityKey
              titleAuthContext.EntityToken = context.TitleAuthenticationContext.EntityToken;
        
              PlayFabEconomyInstanceAPI api = new PlayFabEconomyInstanceAPI(apiSettings,titleAuthContext);
        
              var result = await api.AddInventoryItemsAsync(new PlayFab.EconomyModels.AddInventoryItemsRequest
              {
                  Item = new PlayFab.EconomyModels.InventoryItemReference
                  {                    
                      AlternateId=new PlayFab.EconomyModels.AlternateId
                      {
                          Type= "FriendlyId",
                          Value= "the FriendlyName of the item"
                      } 
                  },
                  Amount = 3,
                  //set the entitykey to the title_player_account of the user you want to modify the inventory for.
                  Entity = new PlayFab.EconomyModels.EntityKey{
                      Id = context.CallerEntityProfile.Entity.Id,
                      Type=context.CallerEntityProfile.Entity.Type
                  }
              }); ;
          }
     }
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.

andrewrasch avatar image andrewrasch commented ·

Ah ok the key here was the requirement of the title player account. It seemed that I wasn't getting that from the Rule trigger and I had to end up using the Profile api to exchange the master player id for the title player id.

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.