Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
371 views
in Technique[技术] by (71.8m points)

Connecting to an API (up42.com) with c#

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:

  1. Set the project ID. PROJ=5a21eaff-cdaa-48ab-bedf-5454116d16ff
  2. Set the project key. PKEY=aoiTOv31.hab0M74qT9cB7K57wO6ue1glddcL3t5zsxb
  3. 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...