InfluxDB and Unity. Able to write, unable to query

I’ve been testing out various database solutions to possibly integrate into my Unity project as means of storing coordinate data (in longitude-latitude pairs). Due to a recommendation from a co-worker I’ve also tried using Influx for this.

Upon testing this out, it seems I am able to write data into a bucket through a script inside Unity, BUT I am somehow not able to read the data from the same bucket. My solution is a 99% copy of the InfluxDB C# tutorial. And as mentioned, the data writing into the bucket works with this solution. When I however try to query data from the bucket I get the following error:

“Error during query: Status(StatusCode=“Unauthenticated”, Detail=“unauthenticated”)”

If I try to run my solution in Visual Studio outside Unity, the querying works. Is there something I am missing or not taking into account (some setting etc) in my Unity solution, or are InfluxDB and Unity just incompatible?

I would greatly appreciate any and all help.

@Topias_Nykanen, a few questions:

  • What version of InfluxDB are you using?
  • Can you provide the code that is querying?

The status code indicates that the query request is not authenticating correctly, but authentication methods depend on the version of InfluxDB you’re using.

Thanks for your reply!

The influx version I’m using is the serverless cloud version 3 (just the simple free-tier for testing purposes). The query code is shown below. (It is pretty much the code provided in the InfluxDB C# tutorial. The content of the table being queried is also based on the census-data from that tutorial)

try
{
// Execute the query and handle the results
await foreach (var row in client.Query(query: sql, database: “buckee”))
{
Debug.Log(row[1]);
}

}
catch (Exception ex)
{
Debug.LogError($“Error during query: {ex.Message}”);
}

I have the InfluxDB3.Client installed in unity through NuGet for Unity- tool. I have the below package installed:

Could this package possibly have something to do with the unauthorizatoin error? Upon searching InfluxDB packages through the tool, there appear to be various other options as well (for example just a plain "InfluxDB.Client).

Thanks!

Hey @Topias_Nykanen & @scott, I’m running into the same problem, and will try to provide more information.

influxdb3 3.0.1, revision d7c071e0c4959beebc7a1a433daf8916abd51214
(installed on local Windows 11 machine)

Unity Editor version 2022.3.61f1
.Net Framework (instead of .Net Standard. Tried both, equivalent behavior.)
InfluxDB3.Client version 1.1.0 (installed through NuGet For Unity)

My code, from an async void MonoBehaviour.Start() function:

const string host = "https://localhost:8181";
const string token = "apiv3_myadminkeyblabla";
const string database = "my_database";

using var client = new InfluxDBClient(host, token: token, database: database);
...
var point = PointData.Measurement("kline");
...
point.SetTag("symbol", testSymbol);
point.SetDoubleField("volume", double.Parse(parts[5]));
point.SetTimestamp(long.Parse(parts[6]), WritePrecision.Us);
...
await client.WritePointAsync(point); // Exception thrown here

The specific exception shows a TLS handshake failure:

TlsException: Handshake failed - error code: UNITYTLS_INTERNAL_ERROR, verify result: UNITYTLS_X509VERIFY_NOT_DONE
Mono.Unity.Debug.CheckAndThrow (Mono.Unity.UnityTls+unitytls_errorstate errorState, Mono.Unity.UnityTls+unitytls_x509verify_result verifyResult, System.String context, Mono.Security.Interface.AlertDescription defaultAlert) (at <17c1853552e149c38adf7a327ade40e6>:0)
Mono.Unity.UnityTlsContext.ProcessHandshake () (at <17c1853552e149c38adf7a327ade40e6>:0)
Mono.Net.Security.MobileAuthenticatedStream.ProcessHandshake (Mono.Net.Security.AsyncOperationStatus status, System.Boolean renegotiate) (at <17c1853552e149c38adf7a327ade40e6>:0)
(wrapper remoting-invoke-with-check) Mono.Net.Security.MobileAuthenticatedStream.ProcessHandshake(Mono.Net.Security.AsyncOperationStatus,bool)
Mono.Net.Security.AsyncHandshakeRequest.Run (Mono.Net.Security.AsyncOperationStatus status) (at <17c1853552e149c38adf7a327ade40e6>:0)
Mono.Net.Security.AsyncProtocolRequest.ProcessOperation (System.Threading.CancellationToken cancellationToken) (at <17c1853552e149c38adf7a327ade40e6>:0)
Rethrow as AuthenticationException: Authentication failed, see inner exception.
Mono.Net.Security.MobileAuthenticatedStream.ProcessAuthentication (System.Boolean runSynchronously, Mono.Net.Security.MonoSslAuthenticationOptions options, System.Threading.CancellationToken cancellationToken) (at <17c1853552e149c38adf7a327ade40e6>:0)
Mono.Net.Security.MonoTlsStream.CreateStream (System.Net.WebConnectionTunnel tunnel, System.Threading.CancellationToken cancellationToken) (at <17c1853552e149c38adf7a327ade40e6>:0)
System.Net.WebConnection.CreateStream (System.Net.WebOperation operation, System.Boolean reused, System.Threading.CancellationToken cancellationToken) (at <17c1853552e149c38adf7a327ade40e6>:0)
Rethrow as WebException: Error: SecureChannelFailure (Authentication failed, see inner exception.)
System.Net.WebConnection.CreateStream (System.Net.WebOperation operation, System.Boolean reused, System.Threading.CancellationToken cancellationToken) (at <17c1853552e149c38adf7a327ade40e6>:0)
System.Net.WebConnection.InitConnection (System.Net.WebOperation operation, System.Threading.CancellationToken cancellationToken) (at <17c1853552e149c38adf7a327ade40e6>:0)
System.Net.WebOperation.Run () (at <17c1853552e149c38adf7a327ade40e6>:0)
System.Net.WebCompletionSource`1[T].WaitForCompletion () (at <17c1853552e149c38adf7a327ade40e6>:0)
System.Net.HttpWebRequest.RunWithTimeoutWorker[T] (System.Threading.Tasks.Task`1[TResult] workerTask, System.Int32 timeout, System.Action abort, System.Func`1[TResult] aborted, System.Threading.CancellationTokenSource cts) (at <17c1853552e149c38adf7a327ade40e6>:0)
System.Net.Http.MonoWebRequestHandler.SendAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) (at <45470e4a4fae4e658de164eff058675f>:0)
Rethrow as HttpRequestException: An error occurred while sending the request
System.Net.Http.MonoWebRequestHandler.SendAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) (at <45470e4a4fae4e658de164eff058675f>:0)
System.Net.Http.HttpClient.SendAsyncWorker (System.Net.Http.HttpRequestMessage request, System.Net.Http.HttpCompletionOption completionOption, System.Threading.CancellationToken cancellationToken) (at <45470e4a4fae4e658de164eff058675f>:0)
InfluxDB3.Client.Internal.RestClient.Request (System.String path, System.Net.Http.HttpMethod method, System.Net.Http.HttpContent content, System.Collections.Generic.Dictionary`2[TKey,TValue] queryParams, System.Collections.Generic.Dictionary`2[TKey,TValue] headers, System.Threading.CancellationToken cancellationToken) (at <16fc6e3eb88a4c35a4252b398f053269>:0)
InfluxDB3.Client.InfluxDBClient.WriteData (System.Collections.Generic.IEnumerable`1[T] data, System.String database, System.Nullable`1[T] precision, System.Collections.Generic.Dictionary`2[TKey,TValue] headers, System.Threading.CancellationToken cancellationToken) (at <16fc6e3eb88a4c35a4252b398f053269>:0)

Note that I’ve tried:

using var client = new InfluxDBClient(host, token: token, database: database, authScheme: "Bearer");

because when I created the admin access token, the console gave this message:

HTTP requests require the following header: "Authorization: Bearer apiv3_myadminkeyblabla"
This will grant you access to HTTP/GRPC API

while I see that in InfluxDbClient.cs the httpClient is created with “Token” as the authScheme by default:

string scheme = (string.IsNullOrEmpty(config.AuthScheme) ? "Token" : config.AuthScheme);
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(scheme, config.Token);

but I’m not sure that this matters. It yields the same error.

More importantly I’ve tried using a garbage token:

using var client = new InfluxDBClient(host, token: "incorrectToken", database: database);

which gives exactly the same error. That leads me to believe the handshake doesn’t get to the point of actually checking the token?

I saw there has been a bug related to this TLS error in slightly older Unity versions, but I get the same problem after upgrading to one beyond the versions containing the fix.

Ah by using influxdb v2, which does have documentation for the server authentication config I learned that the server doesn’t launch with https/TLS enabled by default.

My problem was fixed by connecting to http:localhost:port instead of using https. This works for influxdb3, too.

I can now insert data into tables. But querying fails with:

RpcException: Status(StatusCode="Internal", Detail="Bad gRPC response. Response protocol downgraded to HTTP/1.1.")

Anyway, V2 works, so I’ll use that.