I have a PowerShell script that I want to rewrite in C# (initially I wanted to call the PowerShell script from C#, but here it turned out that rewriting it is propably easier and more elegant).
So this is the PowerShell code I need to port to C#:
$uri = "$BaseUri/auth/token"
$bodyJson = ConvertTo-Json @{token = $ApiToken} -Compress
$response = Invoke-RestMethod `
-Uri $uri `
-Method Post `
-ContentType "application/json" `
-Body $bodyJson
$jwtToken = $response.token
#jwtToken is then used to authenticate a GET request:
$response = Invoke-RestMethod `
-Uri $uri `
-Method Get `
-ContentType "application/json" `
-Authentication Bearer `
-Token $jwtToken `
-AllowUnencryptedAuthentication
This is the C# equivalent I came up with:
//this is only called once
//ApiToken only has a field called "token", the class only exists for the JSON parser to work
ApiToken apiToken = new ApiToken();
client.BaseAddress = new Uri(baseUri);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("token", apiToken.Token);
//this is called every time
Task <HttpResponseMessage> postTask = client.PostAsJsonAsync("auth/token", apiToken);
HttpResponseMessage response = await postTask;
jwt = response.???
Multiple problems here:
- I am new to PowerShell and HttpRequesting and I didn't write the script, so I don't fully understand every little detail here
- I don't know how to retrieve the JWT returned by the API, as I can't use
response.token
in C# (why does that even work in PowerShell? Why doesresponse
have a field called token?) - The C# code returns Error 401 (Unauthorized), while the PowerShell works fine with the same token. My theory is that this happens because I think I don't send the token correctly. (I'm not sure if my C# message matches the PowerShell
ConvertTo-Json @{token = $ApiToken} -Compress
) I feel like I didn't really find the proper equivalent for the-Token
parameter thatInvoke-RestMethod
has.