question

Brandon Wilcox avatar image
Brandon Wilcox asked

Can a TitleId based EntityToken be used to call /Event/WriteEvents and /Event/WriteTelemetryEvents?

I'm working on a Lua based client and am using Azure Functions as part of my analytics pipeline. The analytical events are batched on the Lua client and send to the Azure Function. The Azure Function then formats the data for PlayFab, and attempts to call /Event/WriteEvents or /Event/WriteTelemetryEvents based on a value I pass up.

The problem that I'm running into is with the EntityToken header that's required by these endpoints. Our analytics pipeline is disassociated with players after an event is queued. This means that after the player's entity information is stored in the event payload, all PlayFab calls, including the one to the Azure Function are made on behalf of the title. This approach works up until it's time to actually make the HTTP call to /Event/WriteEvents and /Event/WriteTelemetryEvents.

From my experimentation it seems that an EntityToken registered on behalf of a player is accepted while an EntityToken registered on behalf of the TitleId is not. Below is the code that is responsible for making the request. Right now, I'm currently passing EntityToken in the request body, which may or may not be required. I'm fairly new to this API so any help is appreciated. You'll notice that I index `entityToken` with `data` inside the MakeRequest body. This is because the EntityToken that's passed is the raw JSON that's returned when /GetEntityToken is called.

// Construct request
let request = {
	EntityToken: entityToken.data,
	Events: events
}

// Call endpoint based on eventType
let endpoint = "Event/WriteTelemetryEvents";
        
if (eventType == Enums.AnalyticEventType.PlayStream) {
	endpoint = "/Event/WriteEvents";
}

// Make request to PlayFab Api
GamePlayFab.MakeRequest(
	GamePlayFab.GetServerUrl() + endpoint,
	request,
	"X-EntityToken",
	entityToken.data,
	function (error, result) {
		if (typeof(result) === undefined) {
			return reject(error);
		} else {
			return resolve(result);
		}
	},
);
10 |1200

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

Brandon Wilcox avatar image
Brandon Wilcox answered

Thank you for your response. I do in fact have the SDK imported. The issue was that I was sending the EntityToken object instead of just the EntityToken string.

10 |1200

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

Made Wang avatar image
Made Wang answered

Title EntityToken can access WriteEvents and WriteTelemetryEvents. However, we cannot see where the issue is based on the current code information. Maybe you can provide more detailed sample code, Or do you have some error messages?

Have you imported the PlayFab Js SDK? If not, you can consider importing the sdk, which makes the api call easier. Please refer the documentation to import PlayFab Js SDK to your Azure Functions. If using PlayFab Js SDK, the sample code would be something like this.

function DoExampleGetTitleEntity() {
    PlayFab.settings.titleId = "[yourTitleId]";
    PlayFab.settings.developerSecretKey = "[yourDeveloperSecretKey]";
    var getEntityTokenRequest = {};
    PlayFabAuthenticationSDK.GetEntityToken(getEntityTokenRequest, GetEntityTokenCallback);
}

var GetEntityTokenCallback = function (result, error) {
    if (result !== null) {
        var writeEventsRequest = {
            "Events": [
                {
                    "Entity": {
                        "Id": "[titleplayerId]",
                        "Type": "title_player_account",
                        "TypeString": "title_player_account"
                    },
                    "EventNamespace": "com.playfab.events.myevnet",
                    "Name": "player_play_level",
                    "Payload": {
                        "level_id": 3,
                        "total_attempts": 100
                    }
                }
            ]
        }
        PlayFabEventsSDK.WriteEvents(writeEventsRequest, writeEventsCallback);
    } else if (error !== null) {

    }
}

var writeEventsCallback = function (result, error) {
    if (result !== null) {

    } else if (error !== null) {

    }

}

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.