I am currently working on my backend for a new game I am developing and stumbled upon the fairly lacking API for service accounts in regards of managing player resources, their inventory, economy etc: Unity Services Web API docs
In the roadmap is a card for server authoritative economy. What is the current state of this, is it nearly done, could be tested, not yet started or what?
I would very much like to be able to have a background worker job that goes through resource transfers (like sending wood, stone etc.to other players) and adds the resources to the players once the transfer has completed. If I made it on the client side, then the client would need to be running all the time during the transfer to invoke the cloud code command for it - not feasible at all, that’s why I want to do it on the server side.
Edit: As it is always with cases like this… I was not aware that the cloud code modules could be called by a service account. This is a workaround I can use, though it would still be great to have the client economy API available for service account usage as well.
Hey,
We’ve created this experimental package for this purpose: UGS API Experimental Package Release!
It contains a pretty raw dump of all existing APIs including the service account ones.
Of course, using the service accounts should only happen from a dedicated server or some other backend, what did you have in mind?
EDIT: you can also use access control to prevent players from hitting economy endpoints - Access Control
Cheers,
cc: @erickb_unity
Hello,
In addition to the UGS API Experimental package linked above, if you have your own backend you can also use a service account to hit the Client API directly and adjust resources/currencies for your players.
How to use stateless tokens with a service account to call the Client API
Economy Client API spec
To have Economy be server authoritative, you can follow the Access control docs posted above by Gab.
Cheers
My backend is an ASP.Net Core one with .Net Aspire for containerization, database connection management etc., not a dedicated server from inside Unity, so the experimental package is not usable there.
What I wonder here is why the services docs say ‘Client’ authentication should be used. To me this means it needs to be an authenticated user using the Authentication Service, not a service account. There are services like Cloud Code which explicitly state that one can use client and service account authentication. What is it now?
The client services docs for client api (Unity Services Web API docs) is not properly listing all supported authorization schemes. We reported this to the team to fix this
You can use a c# api generator with the open api spec to generate something that can be used in your ASP .net backend.
2 Likes
You have the option of using a NuGet package:
with the sever authentication or generate a version yourself.
Here’s how:
First download the open API spec generator: CLI Installation | OpenAPI Generator (Also need java if its not installed)
Second, download the economy specs: Unity Services Web API docs (https://services.docs.unity.com/specs/v2/65636f6e6f6d79.yaml) as “economy-client.yaml”
Third, save this as config.yaml:
generatorName: csharp
generatorName: csharp
packageName: Unity.Services.Client.EconomyApi.Generated
packageVersion: "2.0.0"
inputSpec: economy-client.yaml
outputDir: out2
At this point your directory should have 3 files:
- config.yaml
- openapi-generator-cli.jar
- economy-client.yaml
Then, run the following
java -jar openapi-generator-cli.jar generate -c config.yaml
This will create a C# solution from the specs you should be able to use.
attached was my output.
9644168–1371584–out.zip (3.33 MB)
1 Like
After having done that adventure, I realized you most likely could just use the NuGet available for the CloudCode packages (no need to generate your own, though the knowledge is useful).
As Erick said, you can use a service token as well.
Thanks GabKBelmonte, I decided to work around it using exactly this Cloud Code nuget package to set the economy balances etc. I marked the thread as resolved.
I think I wouldnt consider that as a workaround, it is an official package. We do have a bug in the economy doc regarding the authentication it supports.
EDIT: to clarify, you can use this NuGet from any ASP.NET server, it doesnt have to run on CloudCode
to clarify, you can use this NuGet from any ASP.NET server, it doesnt have to run on CloudCode
The end result needs to be compiled to a module and deployed there or not? How else would I get the service token, context etc. for calling the endpoint?
Anyways, I very much appreciate your helpfulness and have found a suitable way for me to continue :).
You should be able to get the token by hitting the right endpoint from the dedicated server hosted the Unity infra (GSH):
GET http://localhost:8086/v4/token
Seems like a case where our doc could be more explicit.
If you’re hosting with your own provider, you could use a service account, and create a token from that.
Edit: I’m also adding the info for completeness, maybe someone else will find it useful if they’re hosting on their own stack