#I guys, i have a question, Cloud Code
1 messages · Page 1 of 1 (latest)
cloud code can also be called from authoritative sources like a server or with a service account (admin)
The unity SDK only supports it being called as a player right now though
I think that I will try to call the cloud code function endpoint with the stateless token for the authorization, let me know if i'm on the right way
Can you help me with this, i don't really know how to authenticate the server, i already have a service account which i use with CLI for managing the authorizations but i'm not sure what do do with my server
I'm trying to call the Run Module Functions but it gaves me this error and i don't know why:
I have already created a ServiceAccount, and created the Token for the bearer authorization
Hi, you have a bit more information in the services docs pages
https://services.docs.unity.com/cloud-code/v1
You need to perform service authentication to get an access token from the service account by doing this operation: https://services.docs.unity.com/docs/service-account-auth/index.html
Admin APIs allow you to access and configure Unity Services as an administrator. To authenticate admin APIs, you must create and use service accounts.
Let me try to get an example payload for you
i have already done it
I have already taken the Bearer token from the token exchange API
BTW calling this cloud code module function from unity with client authoritazion works
From the SDK not a direct WebRequest
I tested it successfully on my side from the editor, let me share the payload for both operations:
POST https://services.api.unity.com/auth/v1/token-exchange?projectId={projectId}&environmentId={environmentId} HTTP/1.1
Host: services.api.unity.com
Accept-Encoding: deflate, gzip
Content-Type: application/json
Accept: application/json
Authorization: Basic {serviceAccount}
Content-Length: 2
POST https://cloud-code.services.api.unity.com/v1/projects/{projectId}/modules/{moduleName}/{functionName} HTTP/1.1
Host: cloud-code.services.api.unity.com
Accept-Encoding: deflate, gzip
Content-Type: application/json
Accept: application/json
Authorization: Bearer {accessToken}
Content-Length: 0
Looks similar to what you have, I didn't see your payload for token exchange, did you specify environment id for that one?
Nope, it wasn't required, but I can try
Cloud code will decode that access token to get he needed information to run your module, environment is required for that afaik
(Also this is the ID and not the name which you can get from the dashboard)
I can also share an experimental package that provides rest api support for ugs that can be useful for this server scenario
ohoh now it works
i have not found something that specifices that cloud code needed that, is it mentioned in the documentation?
Adding the environmentId fix that
If you can share it, i will be happy to try it
Documentation is lacking for these advanced workflows unfortunately
Or we can simply set the environmentId as a required option😂
I remember that when using PlayFab for example, i could inject the id and secret key directly in the editor and then activate and deactivate client and admin API based on the Server or Client Build
The service authentication can be used for other services where environment isn't required, it's a very generic authentication api
The experimental package for rest apis works the same way. Server + Admin support in editor. It's not released yet so any feedback is appreciated
It's an alpha package or it's already stable and needs only a few correction to be released? For sure i can try it and report if i get some issues
It's in the process for an preview/experimental release. Sent you a DM
Thanks, just received, i will comeback to you if i need something, for now thanks a lot
Hi Erick, I'm trying the preview package that you gave me yesterday and i reached a point in which i don't know how to retrive the cloud code function return string
And let me know if i'm signin in in the right way for the server
SignInFromServer uses a method that only works on Multiplay game server hosting, I will need to double check if that authorization method works with cloud code.
If you are hosting somewhere else, you will need to use the SignInWithServiceAccount instead
For content, normally you can rely on response.Data but it does not seem to deserialize correctly in some cases, you'll have to use the raw content directly for now
Here's a quick example of format:
public class MyModule
{
public class Test
{
public int IntValue = 5;
public string StringValue = "hello";
}
[CloudCodeFunction("SayHello")]
public Test Hello()
{
return new Test();
}
}
Debug.Log(runModuleResponse.Content);
=>
{"output":{"intValue":5,"stringValue":"hello"}}
I'll investigate on why the Data field is not deserializing properly
I'm using SignInFromServer from a Unity instance on a Multiplay Server as it should be, i was just don't understanding how to retrive the data. I will try with the example and see if it works
Hi Erick, Everything is right with cloud code, but now i'm trying to log in as TrustedClient but i don't understand if i did it or not, because my access Token is always empty:
Even using wrong credentials doesn't give me errors
These functions are not awaitables, maybe they are not implemented yet?
SignInWithServiceAccount should be awaitable.
The documentation also explains the different ways you can interact with the ApiOperation (You can use Task async/await, callbacks or coroutines)
Btw the trustedClient is more for editor tooling. I can also share some example of editor tooling if you want an example to base yourself on
Indeed i'm using it for this, editor Tooling, as you can see it is not awaitable
That seems to say your method was not marked as async
oh shit you are right, what a stupid
Happens 😉
I should be able to share a new version of the package that will make it easier to work with the cloud code output response.
I'm working on something like that, i can show you if you like, for example adding items to a player for testing purpose, using Trusted client only in editor👍
Awesome, that's exactly the purpose of these apis
Editor tooling for testing, support, validation, observability, etc.
I've made some similar tools in the editor using this
It's a work in progress given the amount of features available though
Woo that's a hell lot of work already done, should be nice to have it in a git repository so it is not production ready but is available if someone need it
I will take it for sure
If you can share it i would be pleased to use it
Do i need to do the sync configuration here? I don't know how to do it with these API
this is how i would do it with the standard client API
The individual SDKs often have wrappers to simplify use which do not exist with the api package. Api package only offers the raw apis so you need to learn how to use those.
You can refer to the api docs page for each service, for example for economy:
https://services.docs.unity.com/economy/v2/#tag/Configuration/operation/getPlayerConfiguration
It's a bit harder to use but provides more flexibility if you need more control (like when making editor tooling with service accounts)
For the admin one: https://services.docs.unity.com/economy-admin/v2/
Got it, thanks, let me know if you can get me the updated package and UI tool
I tried to create a new player account from trusted client client but it was forbidden, indeed i discovered that Admin API don't have and endpoint for that, so i will use ApiService.CreateGameClient(); and that's ok. But i don't understrand why there is a SignUpWithUsernamePassword possibility also on the "ITrustedClient" interface
There is one operation from the player authentication game api that can be used in a trusted context (with a service account)
It's the server-to-server login (SignInWithCustomID)
It's offering the same api in both clients
Each api is based on a service's open api spec and will list all operations for that
The game/trusted/admin/server clients are just wrappers that simplify the authentication and authorization for those apis
Ok so for now the only one working with this wrapper is the SignInWithCustomID?
In trusted context
That's the only one that supports that authorization from what I can see right now
https://services.docs.unity.com/player-auth/v1/#tag/Player-Authentication/operation/SignInWithCustomID
👍
I'm currently using the triggers for receiving events from when a player sign up, but i notice that i can not see the list of triggers i already loaded as i can do for my resource policy by using CLI, are triggers overwrited everytime i load a new file or are they like the resource policies, in which they are updated or added?
No... seems like it works as access policies because i loaded two trigger files with different triggers' name and both started, so now i have to figure out how to delete the first one but i don't remember the name of the first trigger, how can i find it?
I did not try triggers yet... but can't you list them using https://services.docs.unity.com/triggers-admin/v1/#tag/Triggers/operation/listTriggerConfigs
ListTriggerConfigs from the admin client
I also don't know the CLI as well
yes, thanks, there is no option to do it by CLI seems, what a shame, only half of the APIs are implemented on the editor (SDK) and CLI😩 , lucky me there is Postman to help😅
as you can see for the CLI, policies have everything you need, triggers not
I'll ask & raise the feedback to the team
They said listing should be part of the next CLI release, no estimated date for this yet though
ok
In the meantime i created a template for Remote config but i don't know how to delete it, do i need to call an Endpoint my self for this too?😵💫
I created this in the Dashboard, so i thought there was a way to do it
I'm not finding the API to do it in the Remote Config
Does not look like they can be deleted or edited at this time... it's a fairly new feature.
I asked to get some clarifications
For new topics, it's better to just ask directly in #archived-unity-gaming-services
I don't know... but i don't think it's a good idea to release this feature if is not complete yet, or am i wrong?😅 , These services seem like a little bit of a mess
That is valid feedback. The good news is that this product is actively being worked on and improved
BTW this is not a critical feature for me so i can work without it, thanks for everything until now👍
Do you have an idea about why is giving me this error: Unauthorized
Why is serviceToken not Authorized?
This function is called from Sign up trigger
I think Remote config does not support the service token
From what I remember, you can use AssignSettingsAsync with no token to retrieve generic settings
oh ok I thought it was for writing to the remote, not read, because i read "Get" in the other function
More doc on that operation here: https://services.docs.unity.com/remote-config-client/v1/#tag/Settings/operation/assignSettings
Unfortunally
Bad Request 422 this time
With AssignSettingsAsync
Token is mandatory for the function
This is what i get with Postman on "Request Player Settings with optional attributes for use in campaign (game overrides)."
I hope user ID means Service account Key Id
no, that's your player id
You don't need any authorization or service account information for this
ok i don't think so, i tried both and none of them work, BTW on the page seems like a servce account key id
From what I remember, attributes are required
ok man it was quicker to manually made a call to the endpoint, at least it works
so the DOC is wrong?
Think so, it needs a default value
sorry to bother you again but i don't undestarnd how to retrive the remaining items from the next page, getInventoryResult always retrive the first 20 items (i want to know how to use this GetNextAsync, i know i can increase the retrived number of items with the option parameter)