ably / ably-dotnet

.NET, MAUI, Xamarin, Mono and Unity client library SDK for Ably realtime messaging service
https://ably.com/download
Apache License 2.0
45 stars 22 forks source link

System.ArgumentException: Can not convert Object to String using CreateTokenRequestObjectAsync() #1268

Closed mclark-ably closed 7 months ago

mclark-ably commented 11 months ago

When using CreateTokenRequestObjectAsync() in a .net minimal API with ASP.NET core an ably-dotnet client is unable to decode the JSON response and throws an error

[ErrorInfo Reason: Error handling Auth URL, token request failed. See the InnerException property for details of the underlying exception. (See https://help.ably.io/error/80019); Code: 80019; StatusCode: 500 (InternalServerError); Href: https://help.ably.io/error/80019; InnerException: System.ArgumentException: Can not convert Object to String.
   at Newtonsoft.Json.Linq.JToken.op_Explicit(JToken value)

when the returned Content-Type is application/json

Status code: 200 OK ---- Response Headers ---- Date: Wed, 29 Nov 2023 12:01:57 GMT Server: Kestrel Transfer-Encoding: chunked Content Type: application/json Encoding: utf-8 Type: Json ---- Response Body ---- {"keyName":"<redacted>","ttl":"01:00:00","capability":{"resources":[{"name":"_","allowedOperations":["_"],"allowsAll":true}]},"clientId":null,"timestamp":"2023-11-29T12:01:57.695532+00:00","nonce":"<redacted>","mac":"<redacted>"}

Workaround is to use the deprecated method CreateTokenRequestAsync and setting the response type to json

┆Issue is synchronized with this Jira Task by Unito

sync-by-unito[bot] commented 11 months ago

➤ Automation for Jira commented:

The link to the corresponding Jira issue is https://ably.atlassian.net/browse/SDK-3965

sacOO7 commented 9 months ago

Link to internal slack discussion -> https://ably-real-time.slack.com/archives/C030C5YLY/p1700826547995949

mclark-ably commented 7 months ago

CreateTokenRequestObjectAsync() returns timestamp that is not accepted by the Ably requestToken endpoint e.g.

{"keyName":"<redacted>","ttl":"02:00:00","capability":{"Resources":[{"Name":"*","AllowedOperations":["*"],"AllowsAll":true}]},"timestamp":"2024-04-05T10:11:00.317152+00:00","nonce":"<redacted>","mac":"<redacted>"}

which results in an error returned to the client Invalid token request body; timestamp must be a valid number; statusCode=400; code=40001

whereas using the deprecated method returns the timestamp as an epoch timestamp that is accepted e.g.

{"keyName":"<redacted>","ttl":7200000,"capability":"{\"*\":[\"*\"]}","timestamp":1712312063326,"nonce":"<redacted>","mac":"<redacted>"}

sacOO7 commented 7 months ago

@mclark-ably is this raised by a customer? We have a link to the internal discussion https://ably-real-time.slack.com/archives/C030C5YLY/p1700826547995949

mclark-ably commented 7 months ago

@sacOO7 Yes, please see internal slack thread - https://ably-real-time.slack.com/archives/C8SPU4589/p1712176190722049