#I guys, i have a question, Cloud Code

1 messages · Page 1 of 1 (latest)

lusty yacht
#

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

fair forge
#

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

fair forge
#

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

fair forge
#

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

lusty yacht
#

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

fair forge
#

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

lusty yacht
#

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?

fair forge
lusty yacht
#

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

fair forge
#

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

fair forge
lusty yacht
#

Documentation is lacking for these advanced workflows unfortunately

fair forge
fair forge
lusty yacht
#

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

fair forge
lusty yacht
#

It's in the process for an preview/experimental release. Sent you a DM

fair forge
#

Thanks, just received, i will comeback to you if i need something, for now thanks a lot

fair forge
#

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

lusty yacht
#

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

fair forge
#

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

fair forge
#

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

fair forge
#

These functions are not awaitables, maybe they are not implemented yet?

lusty yacht
#

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

fair forge
lusty yacht
#

That seems to say your method was not marked as async

fair forge
#

oh shit you are right, what a stupidnotlikethis

lusty yacht
#

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.

fair forge
lusty yacht
#

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

fair forge
fair forge
fair forge
#

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

lusty yacht
#

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)

fair forge
#

Got it, thanks, let me know if you can get me the updated package and UI tool

fair forge
#

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

lusty yacht
#

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

fair forge
#

Ok so for now the only one working with this wrapper is the SignInWithCustomID?

#

In trusted context

lusty yacht
fair forge
#

👍

fair forge
#

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?

fair forge
#

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?

lusty yacht
#

ListTriggerConfigs from the admin client

#

I also don't know the CLI as well

fair forge
#

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

lusty yacht
#

I'll ask & raise the feedback to the team

lusty yacht
#

They said listing should be part of the next CLI release, no estimated date for this yet though

fair forge
#

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

lusty yacht
#

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

fair forge
lusty yacht
#

That is valid feedback. The good news is that this product is actively being worked on and improved

fair forge
fair forge
#

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

lusty yacht
#

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

fair forge
#

oh ok I thought it was for writing to the remote, not read, because i read "Get" in the other function

fair forge
#

Bad Request 422 this time

#

With AssignSettingsAsync

fair forge
fair forge
#

I hope user ID means Service account Key Id

lusty yacht
#

no, that's your player id
You don't need any authorization or service account information for this

fair forge
lusty yacht
#

From what I remember, attributes are required

fair forge
fair forge
lusty yacht
#

Think so, it needs a default value

fair forge
#

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)