Problem Creating a Player in Cloud Code Script

Hi folks,

I am trying to create a player using the Auth API in a Cloud Code script based upon based upon the CURL example here, which works fine when ran from https://reqbin.com/curl. My project is correctly linked to the IDE, and I have created a Service Account for the app with full permissions.

8859507--1208682--upload_2023-3-7_15-40-4.png

I converted this to an axios call in a Cloud Code script.

const axios = require("axios-0.21");

module.exports = async ({ params, context, logger }) => {
  const { projectId, playerId, environmentId } = context;

  let config = {
    headers: {
      'ProjectId': '0dde1dd5-2e55-41e7-9817-4d67dc48df47',
    }
  };
  const url = 'https://player-auth.services.api.unity.com/v1/authentication/anonymous';
  const response = await axios.post(url, config);
  return await response.data;
};

When ran from the Cloud Code dashboard, I get the following error:

Invocation Error
------------------------------
Error: Request failed with status code 400

{
  "message": "Request failed with status code 400",
  "name": "Error",
  "request": {
    "data": "{\"headers\":{\"ProjectId\":\"0dde1dd5-2e55-41e7-9817-4d67dc48df47\"}}",
    "headers": {
      "Accept": "application/json, text/plain, */*",
      "Content-Length": 64,
      "Content-Type": "application/json",
      "User-Agent": "axios/0.21.4"
    },
    "method": "post",
    "url": "https://player-auth.services.api.unity.com/v1/authentication/anonymous"
  },
  "response": {
    "detail": "Valid ID Domain not specified",
    "details": [],
    "status": 400,
    "title": "INVALID_PARAMETERS"
  }
}

When ran from the Unity IDE using the C# code below, I get the following error.

public class CreateUserResponse
    {
        public string userId;
        public string idToken;
        public string sessionToken;
        public int expiresIn;
        public User user;
    }

    public class User
    {
        public string id;
        public bool disabled;
        public List<object> externalIds;
    }

    public async void CreateAnonUser()
    {
        var arguments = new Dictionary<string, object> { };
        var result = await CloudCodeService.Instance.CallEndpointAsync<CreateUserResponse>("CreateAnonUser", arguments);
    }
ArgumentException: Failed to lookup discriminator value for problems/invocation/axios. Possible values: problems/basic, problems/invocation, BasicErrorResponse, InvocationErrorResponse
Unity.Services.CloudCode.Internal.Models.RunScript422OneOf.GetConcreteType (System.String type) (at Library/PackageCache/com.unity.services.cloudcode@2.2.4/Runtime/com.unity.services.cloudcode.internal/Models/RunScript422OneOf.cs:69)
Unity.Services.CloudCode.Internal.Models.RunScript422OneOf.DeserializeIntoActualObject (System.String discriminatorValue, System.String jsonString) (at Library/PackageCache/com.unity.services.cloudcode@2.2.4/Runtime/com.unity.services.cloudcode.internal/Models/RunScript422OneOf.cs:105)
Unity.Services.CloudCode.Internal.Models.RunScript422OneOf.FromJson (System.String jsonString) (at Library/PackageCache/com.unity.services.cloudcode@2.2.4/Runtime/com.unity.services.cloudcode.internal/Models/RunScript422OneOf.cs:98)
Unity.Services.CloudCode.Internal.Models.RunScript422OneOfJsonConverter.ReadJson (Newtonsoft.Json.JsonReader reader, System.Type objectType, System.Object existingValue, Newtonsoft.Json.JsonSerializer serializer) (at Library/PackageCache/com.unity.services.cloudcode@2.2.4/Runtime/com.unity.services.cloudcode.internal/Models/RunScript422OneOf.cs:165)
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.DeserializeConvertable (Newtonsoft.Json.JsonConverter converter, Newtonsoft.Json.JsonReader reader, System.Type objectType, System.Object existingValue) (at /root/repo/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs:2190)
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType, System.Boolean checkAdditionalContent) (at /root/repo/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs:202)
Newtonsoft.Json.JsonSerializer.DeserializeInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType) (at /root/repo/Src/Newtonsoft.Json/JsonSerializer.cs:904)
Newtonsoft.Json.JsonSerializer.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType) (at /root/repo/Src/Newtonsoft.Json/JsonSerializer.cs:883)
Newtonsoft.Json.JsonConvert.DeserializeObject (System.String value, System.Type type, Newtonsoft.Json.JsonSerializerSettings settings) (at /root/repo/Src/Newtonsoft.Json/JsonConvert.cs:831)
Unity.Services.CloudCode.Internal.Http.ResponseHandler.TryDeserializeResponse (Unity.Services.CloudCode.Internal.Http.HttpClientResponse response, System.Type type) (at Library/PackageCache/com.unity.services.cloudcode@2.2.4/Runtime/com.unity.services.cloudcode.internal/Http/ResponseHandler.cs:66)
Unity.Services.CloudCode.Internal.Http.ResponseHandler.CreateOneOfException (Unity.Services.CloudCode.Internal.Http.HttpClientResponse response, System.Type responseType) (at Library/PackageCache/com.unity.services.cloudcode@2.2.4/Runtime/com.unity.services.cloudcode.internal/Http/ResponseHandler.cs:109)
Rethrow as ResponseDeserializationException: Failed to lookup discriminator value for problems/invocation/axios. Possible values: problems/basic, problems/invocation, BasicErrorResponse, InvocationErrorResponse
Unity.Services.CloudCode.Internal.Http.ResponseHandler.CreateOneOfException (Unity.Services.CloudCode.Internal.Http.HttpClientResponse response, System.Type responseType) (at Library/PackageCache/com.unity.services.cloudcode@2.2.4/Runtime/com.unity.services.cloudcode.internal/Http/ResponseHandler.cs:114)
Unity.Services.CloudCode.Internal.Http.ResponseHandler.HandleAsyncResponse (Unity.Services.CloudCode.Internal.Http.HttpClientResponse response, System.Collections.Generic.Dictionary`2[TKey,TValue] statusCodeToTypeMap) (at Library/PackageCache/com.unity.services.cloudcode@2.2.4/Runtime/com.unity.services.cloudcode.internal/Http/ResponseHandler.cs:103)
Unity.Services.CloudCode.Internal.Http.ResponseHandler.HandleAsyncResponse[T] (Unity.Services.CloudCode.Internal.Http.HttpClientResponse response, System.Collections.Generic.Dictionary`2[TKey,TValue] statusCodeToTypeMap) (at Library/PackageCache/com.unity.services.cloudcode@2.2.4/Runtime/com.unity.services.cloudcode.internal/Http/ResponseHandler.cs:186)
Unity.Services.CloudCode.Internal.Apis.CloudCode.CloudCodeApiClient.RunScriptAsync (Unity.Services.CloudCode.Internal.CloudCode.RunScriptRequest request, Unity.Services.CloudCode.Internal.Configuration operationConfiguration) (at Library/PackageCache/com.unity.services.cloudcode@2.2.4/Runtime/com.unity.services.cloudcode.internal/Apis/CloudCodeApi.cs:101)
Unity.Services.CloudCode.CloudCodeInternal.GetResponseAsync (System.String function, System.Collections.Generic.Dictionary`2[TKey,TValue] args) (at Library/PackageCache/com.unity.services.cloudcode@2.2.4/Runtime/CloudCode.cs:176)
Unity.Services.CloudCode.CloudCodeInternal.GetRunScriptResponse (System.String function, System.Collections.Generic.Dictionary`2[TKey,TValue] args) (at Library/PackageCache/com.unity.services.cloudcode@2.2.4/Runtime/CloudCode.cs:79)
Rethrow as CloudCodeException: Unknown
Failed to lookup discriminator value for problems/invocation/axios. Possible values: problems/basic, problems/invocation, BasicErrorResponse, InvocationErrorResponse
Failed to lookup discriminator value for problems/invocation/axios. Possible values: problems/basic, problems/invocation, BasicErrorResponse, InvocationErrorResponse
Unity.Services.CloudCode.CloudCodeInternal.GetRunScriptResponse (System.String function, System.Collections.Generic.Dictionary`2[TKey,TValue] args) (at Library/PackageCache/com.unity.services.cloudcode@2.2.4/Runtime/CloudCode.cs:99)
Unity.Services.CloudCode.CloudCodeInternal.CallEndpointAsync[TResult] (System.String function, System.Collections.Generic.Dictionary`2[TKey,TValue] args) (at Library/PackageCache/com.unity.services.cloudcode@2.2.4/Runtime/CloudCode.cs:43)
CloudCodeManager.CreateAnonUser () (at Assets/Scripts/CloudCodeManager.cs:114)
System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__7_0 (System.Object state) (at <1f66344f2f89470293d8b67d71308c07>:0)
UnityEngine.UnitySynchronizationContext+WorkRequest.Invoke () (at <4014a86cbefb4944b2b6c9211c8fd2fc>:0)
UnityEngine.UnitySynchronizationContext.Exec () (at <4014a86cbefb4944b2b6c9211c8fd2fc>:0)
UnityEngine.UnitySynchronizationContext.ExecuteTasks () (at <4014a86cbefb4944b2b6c9211c8fd2fc>:0)

This isn't something I've done a ton of in the past so I may be missing something obvious, but I'd appreciate anyone's help pointing me in the correct direction.

Thank you!
Travis

I haven't mucked with the Auth APIs yet, but just by glancing over the error and your code, it looks like you're not actually wrapping the "ProjectId: 0dde1dd5-2e55-41e7-9817-4d67dc48df47" into the headers of the POST request.

From reqbin website, you actually did put that into the headers.

It's also evident in the Request error that you actually put the "headers" into the content of the request:

"request": {
    "data": "{\"headers\":{\"ProjectId\":\"0dde1dd5-2e55-41e7-9817-4d67dc48df47\"}}",
    "headers": {
      "Accept": "application/json, text/plain, */*",
      "Content-Length": 64,
      "Content-Type": "application/json",
      "User-Agent": "axios/0.21.4"
    },
    "method": "post",
    "url": "https://player-auth.services.api.unity.com/v1/authentication/anonymous"
  }

Ah man, thanks for that. I was missing the data parameter in the axios call. Now to figure out what to put there to make it work.

Correction, passing an empty object did the trick. Thanks again!