question

andymaddison avatar image
andymaddison asked

Where to set DeveloperSecretKey for Azure PlayFabServerAPI calls

I'm trying to call a PlayFabServerAPI method in my cloud script but get this message when it fails.

DeveloperSecretKey must be set in your local or global settings to call this method

I have tried adding "DeveloperSecretKey":"[my key]" to local.settings.json but it didn't help. VS Code is telling me it's not a valid key for settings.json so I haven't even tried that. Also tried adding to the Application Settings list in the Azure extension.

Where do I set it?

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

Seth Du avatar image Seth Du ♦ commented ·

Thanks for the feedback. We will try to reproduce the issue and will keep you informed.

0 Likes 0 ·
JayZuo avatar image
JayZuo answered

The example code you've mentioned is out of date. Currently, what we can get from function execution context are listed here: https://docs.microsoft.com/en-us/gaming/playfab/features/automation/cloudscript-af/cloudscript-af-context. In the context we can not get DeveloperSecretKey. So to use Server API, we will need to add an Application settings in Azure function.

For example, we can add a new key-value pair with the name set to "PLAYFAB_DEV_SECRET_KEY" and the value set to the Secret key listed in PlayFab Game Manager.

Then in Azure Function, we can get DeveloperSecretKey with the following code:

Environment.GetEnvironmentVariable("PLAYFAB_DEV_SECRET_KEY", EnvironmentVariableTarget.Process);

More details can be found at https://docs.microsoft.com/en-us/azure/azure-functions/functions-dotnet-class-library#environment-variables.

Once you've got the DeveloperSecretKey, you can either use PlayFabServerAPI or PlayFabServerInstanceAPI. PlayFabServerInstanceAPI is newly introduced, allowing multiple instances with different settings or authentication contexts in one project.

For PlayFabServerAPI, the code would like:

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

...

var result = await PlayFabServerAPI.GetLeaderboardAsync(request);

And for PlayFabServerInstanceAPI, the code would be like:

var apiSettings = new PlayFabApiSettings
{
    TitleId = context.TitleAuthenticationContext.Id,
    DeveloperSecretKey = Environment.GetEnvironmentVariable("PLAYFAB_DEV_SECRET_KEY", EnvironmentVariableTarget.Process),
};
var serverApi = new PlayFabServerInstanceAPI(apiSettings);
var result = serverApi.GetLeaderboardAsync(request);
5 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.

andymaddison avatar image andymaddison commented ·

I ended up finding the Handlers sample code on Github but I notice that it does it a different way to your HelloWorld example.

AuthorizationLevel.Function vs AuthorizationLevel.Anonymous

HttpRequestMessage vs HttpRequest

FunctionContext vs FunctionExecutionContext

Which way do you recommend I use as a base for my own functions?

Also, the sample code does not work because it gets the PlayFabId from context.CurrentPlayerId which ends up being the entity id rather than the player id. I had to pass in the player id via parameters in order to get it to work correctly. Or am I missing something here?

0 Likes 0 ·
JayZuo avatar image JayZuo ♦ andymaddison commented ·

As I've said, the sample is is out of date. You should use FunctionExecutionContext now. Or if you are using dynamic type, you can just using some code like the following:

0 Likes 0 ·
JayZuo avatar image JayZuo ♦ andymaddison commented ·
public static async Task<dynamic> UpdatePlayerStatistics(
    [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
    ILogger log)
{
    dynamic context = JsonConvert.DeserializeObject(await req.ReadAsStringAsync());
    log.LogInformation($"Function execution's context: {context}");
    PlayFabSettings.staticSettings.TitleId = context.TitleAuthenticationContext.Id;
    PlayFabSettings.staticSettings.DeveloperSecretKey = Environment.GetEnvironmentVariable("PLAYFAB_DEV_SECRET_KEY", EnvironmentVariableTarget.Process);
    var args = context.FunctionArgument;
    log.LogInformation($"StatisticName: {args.StatisticName} & Value: {args.Value}");
    var request = new UpdatePlayerStatisticsRequest
    {
        PlayFabId = context.CallerEntityProfile.Lineage.MasterPlayerAccountId,
        Statistics = new List<StatisticUpdate>
        {
            new StatisticUpdate
            {
                StatisticName = args.StatisticName,
                Value = args.Value
            }
        }
    };
    return await PlayFabServerAPI.UpdatePlayerStatisticsAsync(request);
}
0 Likes 0 ·
andymaddison avatar image andymaddison JayZuo ♦ commented ·

Thanks for that code Jay.

0 Likes 0 ·
omerkhalid avatar image omerkhalid commented ·

Just change Constants.PLAYFAB_DEV_SECRET_KEY to "PLAYFAB_DEV_SECRET_KEY". I stored it in a constant which i used here

0 Likes 0 ·
andymaddison avatar image
andymaddison answered

I'm getting really frustrated in trying to get started with Azure Cloudscript on Playfab.

I found info about setting the key, which I did. And I no longer get the message about DeveloperSecretKey.

"PLAYFAB_DEV_SECRET_KEY": "[INSERT PLAYFAB TITLE DEV SECRET KEY HERE]",

It's also not clear how I make server calls from within the script.

Is it like this?

var result = await PlayFabServerAPI.GetLeaderboardAsync(request);

Or this?

var serverApi = new PlayFabServerInstanceAPI();

var result = await serverApi.GetLeaderboardAsync(request);

The example code on Github doesn't even compile. What is FunctionContext and where do I find it?

var context=await FunctionContext<dynamic>.Create(req);

var serverApi=new PlayFabServerInstanceAPI(context.ApiSettings, context.AuthenticationContext);

var result = await serverApi.GetLeaderboardAsync(request);

Nothing seems to work for me. GetLeaderboardAsync gets called but never completes.

I could do with some working example code. Is there any available?


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.

omerkhalid avatar image omerkhalid commented ·

Hi @andymaddison. I was stuck too but this works. Hopefully this helps anybody coming across this question because its the end of 2020 and the code in documentation still doesn't compile

  public class YourFuncName
{
[FunctionName("YourFuncName")]
public async Task<dynamic> Run( [HttpTrigger(AuthorizationLevel.Function, "get","post", Route = null)] HttpRequest req, ILogger log) { string playerId = string.Empty; var context = JsonConvert.DeserializeObject<FunctionExecutionContext<dynamic>>(await req.ReadAsStringAsync()); dynamic args = context.FunctionArgument; PlayFabSettings.staticSettings.TitleId = context.TitleAuthenticationContext.Id; PlayFabSettings.staticSettings.DeveloperSecretKey = Environment.GetEnvironmentVariable(Constants.PLAYFAB_DEV_SECRET_KEY, EnvironmentVariableTarget.Process); var result1 = await PlayFabServerAPI.GetTitleDataAsync(new GetTitleDataRequest()); return result1; } }
0 Likes 0 ·
eparesignups avatar image eparesignups omerkhalid commented ·

Been working with this sdk but the examples are so out of date. I've tried this and can't seem to get it to work.

Are we supposed to put the actual secret key from the playfab game manager into the local.settings.json or are we supposed to put the Azure app setting key that holds the secret key value?

Should we touch the example class properties?

I keep getting: "Could not fetch the developer secret key from the environment. Please set "PLAYFAB_DEV_SECRET_KEY" in your app's local.settings.json file. But i've already tried setting the actual key from playfab and the app setting name from azure.

0 Likes 0 ·
omerkhalid avatar image
omerkhalid answered

Hi. I am sorry I should have elaborated further. I have a static class called "Constant" which has a variable called "PLAYFAB_DEV_SECRET_KEY". What you can do is create a variable "PLAYFAB_DEV_SECRET_KEY:YOUR PLAYFAB DEV SECRET ID" in local.settings.json and publish this. Azure will pick it up itself and then use it like this in your code

 PlayFabSettings.staticSettings.DeveloperSecretKey = Environment.GetEnvironmentVariable("PLAYFAB_DEV_SECRET_KEY", 
                                                                                                    EnvironmentVariableTarget.Process);
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.

eparesignups avatar image eparesignups commented ·

Thanks! I was able to work through this.

1 Like 1 ·

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.