Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Question Can service web api be used?

Discussion in 'Unity User-Generated Content' started by unity_4CCAB079EC420FB17022, Jul 27, 2023.

  1. unity_4CCAB079EC420FB17022

    unity_4CCAB079EC420FB17022

    Joined:
    Aug 8, 2022
    Posts:
    9
    I try to follow service account authentication doc to generate service account credential(base64 of key + secret) and used it to call ugc api, but most apis that need to be authenticated fail.

    Code (CSharp):
    1. curl -X PUT \
    2.   -H "Authorization: Basic <service-account-credential>" \
    3. https://ugc.services.api.unity.com/v1/projects/:projectId/environments/:environmentId/content/:contentId/visibility
    4.  
    Calling with proper variable substitution will result failiure

    Code (CSharp):
    1. {
    2.     "status": 401,
    3.     "title": "Unauthorized",
    4.     "type": "https://services.docs.unity.com/docs/errors/#51",
    5.     "requestId": "40fc4663-f97b-49ea-ad6f-15a099b6e22c",
    6.     "detail": "Invalid Authorization header",
    7.     "code": 51
    8. }
    Do I miss anything? Please help.
     
  2. unity_4CCAB079EC420FB17022

    unity_4CCAB079EC420FB17022

    Joined:
    Aug 8, 2022
    Posts:
    9
    I also tried to do token-exchange, but I don't know what scope should I use. Tried with "user_generated_content.admin.all" but it does not work, I keep getting

    Code (CSharp):
    1. {
    2.     "name": "AuthorizationError",
    3.     "message": "The service account does not have access to all of the requested scopes.",
    4.     "status": 403,
    5.     "code": 53,
    6.     "type": "https://services.docs.unity.com/docs/errors/#53"
    7. }
    But using "remote_config.configs.create", "multiplay.allocations.create", etc. as scopes will get me the token.

    Not sure what to do at this point, as I just want to make ugc content upload during editor time. Since sdk seems only can be used at runtime, I am thinking to use api directly during editor script. But none of the api that requires auth can be used.
     
  3. anthonylord

    anthonylord

    Unity Technologies

    Joined:
    Dec 8, 2022
    Posts:
    2
    Hello,

    When using a service account, you can only access endpoints from the Service Gateway (Admin API)
    In your example, you are trying to access an endpoint in the Game Gateway (Client API)

    If you retry your initial request with this URL instead : https://services.api.unity.com/ugc/v1/.......
    It should work.

    While investigating I found that we were providing the wrong URL for our Admin API docs, which might be why you got confused. It will be fixed shortly.

    This is the doc you should be using : https://services.docs.unity.com/use...tag/Content/operation/UpdateContentVisibility

    Except the base URL to call needs to be https://services.api.unity.com/ugc/v1/...

    Hopefully this answers your questions.
     
  4. FlyingSquirrels

    FlyingSquirrels

    Joined:
    Sep 18, 2015
    Posts:
    82
    Hey @anthonylord ,

    We are having a similar problem and hoping you can help us out too.

    The problem/goal:
    We are trying to use the client facing Multiplay Game Server Lifecycle APIs listed here:
    Unity Services Web API docs

    E.g. https://multiplay.services.api.unit.../{environmentId}/fleets/{fleetId}/allocations

    It is not clear to us from the documentation how authentication should be done so we've been trying a few approaches using a WebRequest as follows:

    Code (CSharp):
    1. private IEnumerator<float> GetServersCoroutine()
    2.     {
    3.         byte[] keyByteArray = Encoding.UTF8.GetBytes(SERVICE_ACCOUNT_KEY_ID + ":" + SERVICE_ACCOUNT_KEY_SECRET);
    4.         string keyBase64 = Convert.ToBase64String(keyByteArray);
    5.         using (UnityWebRequest www = UnityWebRequest.Get(_getServersURL))
    6.         {
    7.             www.SetRequestHeader("Authorization", "Basic " + keyBase64);
    8.             www.SendWebRequest();
    9.             while (!www.isDone)
    10.             {
    11.                 //Debug.Log($"Waiting for callback. {www.downloadProgress}");
    12.                 yield return Timing.WaitForOneFrame;
    13.             }
    14.             Debug.Log($"GetServers => Error: {www.error}. Bytes: {www.downloadedBytes}. Result: {www.result}");
    15.         }      
    16.     }
    Right now:
    1. We've tried with a service account and we get 'Not authenticated error'
    2. When we try with a client account, the IAuthenticationService interface has no TokenID as described here.

    The question:
    1. When Unity says this is a Client API, should we be using a service account or client auth like 'AuthenticationService.Instance.SignInAnonymouslyAsync'?
    2. Would it be possible for Unity to share a WebRequest example on how to call a Web API such as the List fleet allocations listed here?
    Thank you!
     
  5. gogogotchi

    gogogotchi

    Joined:
    Mar 21, 2023
    Posts:
    10
    Same here - did you figure this out?