question

nathanielj avatar image
nathanielj asked

Authentication as a server to make API calls

I am making an Unreal racing game with dedicated servers. My idea is to have the players join a dedicated PlayFab server and RPC their Character IDs to the server. Then the server makes an API call to PlayFab for each CharacterId, receiving their character data which the server turns into custom car configurations. The server then spawns the cars and replicates to all clients.

This works fine for clients and listen servers, where the API calls are being made with the already logged-in player's Custom ID.

However, I receive this error when attempting to make API calls as a dedicated server:

LogPlayFab: Response : {"code":401,"status":"Unauthorized","error":"NotAuthenticated","errorCode":1074,"errorMessage":"Not Authenticated"}

I'm currently testing on my computer, starting the dedicated server with the -noplayfab command so it doesn't crash.

What is the proper way to use the API like this, for testing off/on the PlayFab multiplayer servers? Do servers end up having logins like players, or is it totally different?

10 |1200

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

Sarah Zhang avatar image
Sarah Zhang answered

@nathanielj Firstly, you don’t need to use the EntityToken to access this server API GetCharacterData. As the API reference showed, the request header only contains SecretKey. So your server can call this API using Secret Key directly. As long as you set the Secret Key, you can access PlayFab Classic Server API methods.

This “Unauthorized” error should be returned from GetEntityToken API. There is a known issue in the PlayFab Market Place Plugin, when you call the GetEntityToken API, our plugin would try to use an EntityToken to authorize the calling, it leads to a paradox. So GetEntityToken API returned this error.

You can modify the PlayFab Plugin’s source code to fix this issue temporarily. Before you modify the Plugin’s code, you need to duplicate the plugin to your UProject’s directory. You can refer to our Unreal Example Project to put the Plugins folder. Then you can navigate to “[YourProjectName]/Plugins/PlayFab/Source/PlayFab/Private/PlayFabAuthenticationAPI.cpp”, modify the line 273 to line 277 to the following code.

if (useEntityToken) 
HttpRequest->SetHeader(TEXT("X-EntityToken"), CallAuthenticationContext
!= nullptr ?
CallAuthenticationContext->GetEntityToken() :
pfSettings->getEntityToken());/*else*/if (useSessionTicket) 
HttpRequest->SetHeader(TEXT("X-Authorization"),
CallAuthenticationContext != nullptr ? CallAuthenticationContext->GetClientSessionTicket() :
pfSettings->getSessionTicket());/*else*/if (useSecretKey)

After you save the code, please build this project. After you built the project, the blueprints of calling GetEntityToken would work fine.

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.

nathanielj avatar image nathanielj commented ·

Thanks, this makes sense and looks like the best solution.

However, I have already tried this client login step (with secret set earlier) as a work around in Blueprints.

Is it also a good workaround for the paradox, to login as a client with the secret and a dummy name?

0 Likes 0 ·
Sarah Zhang avatar image Sarah Zhang nathanielj commented ·

If you have set the entity policy to give this player the highest access, it would be a workaround.

0 Likes 0 ·
Sarah Zhang avatar image
Sarah Zhang answered

Could you please tell us, what are API methods you tried to call? And what is the development platform of your server, is it Unreal?

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.

nathanielj avatar image nathanielj commented ·

The API method I tried to call was GetCharacterData, both as a client and server

I got onto the PlayFab slack and received some help there. I was told that the server needs to use GetEntityToken to authenticate itself with the secret key.

The development platform is Unreal and the team is using Blueprints as much as possible. Unfortunately, I was also told that the Blueprint node for GetEntityToken is broken. Related error: https://community.playfab.com/questions/37781/getmatch-server-authentication-error.html

This C++ code was provided to me as a workaround: https://pastebin.com/2D36L88K

0 Likes 0 ·
nathanielj avatar image nathanielj commented ·

I would greatly prefer if the Blueprint method worked.

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.