I am trying to connect to an API.
I am following this walkthrough: https://docs.up42.com/going-further/api-walkthrough.html
I need to generate a token:
- Set the project ID.
PROJ=5a21eaff-cdaa-48ab-bedf-5454116d16ff
- Set the project key.
PKEY=aoiTOv31.hab0M74qT9cB7K57wO6ue1glddcL3t5zsxb
- Get the token.
PTOKEN=$(curl -sX POST "https://$PROJ:[email protected]/oauth/token" -H 'Content-Type: application/x-www-form-urlencoded' -d 'grant_type=client_credentials' | jq -r '.data.accessToken')
This is the c# code I've been trying to use (from https://curl.olsh.me/):
using (var httpClient = new HttpClient())
{
using (var request = new HttpRequestMessage(new HttpMethod("POST"), "https://$PROJ:[email protected]/oauth/toke")) //$PROJ and $PKEY replaced with my Project ID and Project key
{
request.Content = new StringContent("grant_type=client_credentials");
request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/x-www-form-urlencoded");
var response = await httpClient.SendAsync(request);
}
}
This is supposed to generate a Token that I can use to make requests but I keep getting a "401 Unauthorized" response.
I've also tried using RestSharp:
RestClient client = new RestClient("https://$PROJ:[email protected]/oauth/token"); //$PROJ and $PKEY replaced with my Project ID and Project key
RestRequest request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddParameter("grant_type", "client_credentials", ParameterType.RequestBody); //I'm fairly certain this is incorrect
JToken token = JToken.Parse(client.Execute(request).Content);
But this also gives me a "401 Unauthorized" response.
I'm kind of at a loss.
question from:
https://stackoverflow.com/questions/65602814/connecting-to-an-api-up42-com-with-c-sharp 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…