Not authorized when trying to use the API calls from .NET

Dear all,
when trying to make a call via the Api, I seem to encounter authentication problems. I use my JWT as follows.

    var client = new SpeckleApiClient(account.RestApi, false, "console_app");
  client.AuthToken = Properties.Resources.JWT;

  var i = 0;
  var totalObjs = 0;
  foreach (var payload in objectUpdatePayloads)
    totalObjs += payload.Count;
    Console.Write(String.Format("Sending payload #{0} ({1} objects, total saved {2}) ...", i++, payload.Count, totalObjs));

    var result = client.ObjectCreateAsync(payload).Result.Resources;

As a reply, I get a 401 error code. One issue that I had previously is that SpeckleCore.SpeckleInitializer.Initialize(); does not work in .NET core. Is not executing the initializer a possible cause for this problem? Or am I missing any other authentication credential?

Many thanks in advance for any advice!

Seems that I have solved the issue, I added the prefix JWT to the Token. Also switched to creating a stream instead of an object for now. The stream is created correctly, however, there is an exception about not being able to deserialize the response body

1 Like

Hello @Simon! Cool to see you’re following up with replies as well. If on windows, I usually use fiddler to actually check what the response looks like when doing this kind of debugging.

I could be more useful if I got to see the error / response from the server, or some code that i can reproduce it with!

Hello Dimitrie, thank you for the quick reply! Here is what the error looks like image

I’m currently working on accessing the response from the server

Uh, wasn’t really a quick reply - but i’m trying. Check out fiddler to see the raw response. Alternatively, if you go into “view details”, the inner exception should contain a bit more info.

I’ve had this in the past, and it’s usually something simple… Have you checked out the sample repo? That one should work out of the box…

Checking out the inner exception gave me this. It seems that GetResultCore fails for some reason.

It displays that some of the reflection is not supported in my environment. Could that be the cause?

What’s your target framework? This usually happens with methods not supported in .net core. AFAIK, the latest pre-release of SpeckleCore should be .net standard proof, but we might have missed something. That being said, make sure you’re using 1.7.12: NuGet Gallery | SpeckleCore 1.7.1-wip

Thank you for the tip, I have switched the PackageReference to Version 1.7.2, but the error is still showing up. we were using SpeckleCore Version 1.6.9 before. As far as I understood the method is still not supported by .Net Core right? do we possibly need to switch our project framework?

Is there a repo somewhere I could use to reproduce this? Feel free to send me an email if it’s private! Or a simple project that i can use to debug would be even more helpful…

Just a quick update on the status. I have created a new project using .NET framework 4.7 with the code and SpeckleCore 1.7.2 to circumvent the error. however it still appears the same way as before.

Seems that a parameter “source” is null, possibly because the reflection methods are not supported. Any ideas which framework I could choose to bypass it and keep going for now? Cheers, Simon

I’m actually debugging now this - managed to get the repo to work. I also believe it’s frameworks incompatibility of sorts. The response is formatted as it should be, it’s valid json - so there’s something fishy there. Digging through it now.

Fingers crossed we get to the bottom of this!

I think it’s because you’re referencing SpeckleCoreGeometry, which in turn references SpeckleCore, which in turn is not working on net core.

I’m trying to update the dependencies, push a new wip release pf SpeckleCoreGeometry, and see what happens…

Oh yes, that is possible, as i also got a notification including a reference to v 1.5… and wondered where that’s from. Sounds great btw!

Okay, digging a bit more. I think the problem is that SpeckleCoreGeometryClasses is still a .net framework project. This makes the initialisation call to core fail when getting types and such.

Not sure this is the root cause, but here’s to hoping: i’m migrating it to .net standard 2, let’s see where we get with that.

Think I’ve nailed it @Simon! For anybody watching, the postmortem of this annoying issue is a lesson in dll and framework targets hell:

  • Somehow, the 1.7.1-wip release of SpeckleCore on nuget came from an older branch and was not actually the latest, .net standard one. 1.7.0-wip actually is. Resolution: unlisted 1.7.1 from nuget.
    This was causing the reflection only load is not supported in this framework error.
  • There’s more: SpeckleCoreGeometryClasses could not be reflected on by core as it was still targeting .net framework, rather than .net standard. Resolution: released 1.3.2 on nuget, which is a .net standard project.

To hopefully resolve your specific issue:

  • downgrade the speckle core nuget to 1.7.0 in all projects (and clean & rebuild!)
  • bump the SpeckleCore geometry nuget to 1.3.2 (clean & rebuild)
  • do intialise speckle core; should now not throw any errors (SpeckleCore.SpeckleInitializer.Initialize();)
  • restart visual studio a couple of times for good measure :smiley:
  • profit!

Note: if you have other kits installed (ie, via the official installer/gsa installer), such as speckle elements, you will probably need to remove them until I manage to get to cross off my todo (below). My dirty solution was to copy paste the SpeckleKits folder (from `%localappdata%) and delete the original.

My todo is to migrate everything that’s speckle “core” and not app client specific to .net standard. Do let me know if the above works!

Yes, that worked, I am successfully creating streams now! :partying_face:
Had to upgrade and downgrade the versions, as well as uninstalling speckle via official installer