Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

c# - Calling a Rest API with Bearer token

I'm trying to consume a REST api with bearer token authentication. I'm getting this error:

Media type is unsupported

Code:

using System;
using RestSharp;
using System.Configuration;
using Newtonsoft.Json.Linq;

 string Authtoken = "My OAuth token";
 var client = new RestClient(DataserviceURL);
 var request = new RestRequest(Method.POST);
 request.AddHeader("Authorization", "Bearer " + Authtoken);

        try
        {
            IRestResponse response = client.Execute(request);

            var obj = JObject.Parse(response.Content);

            Console.WriteLine("Data_" + response.Content);
            Console.ReadLine();
        }
        catch (Exception ex) { string ex1 = ex.ToString(); }
question from:https://stackoverflow.com/questions/65830495/calling-a-rest-api-with-bearer-token

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

1 Answer

0 votes
by (71.8m points)

You most likely just need to add a HTTP header like this:

var request = new RestRequest(Method.POST);
request.Headers.Add("Content-Type", "application/json");

to clarify what type of content you're sending in your POST body (that is assuming you are sending JSON in your POST body - otherwise adapt as needed). This has nothing to do with your bearer token authentication ...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...