简体   繁体   English

使用 Bearer 令牌调用 Rest API

[英]Calling a Rest API with Bearer token

I'm trying to consume a REST api with bearer token authentication.我正在尝试使用带有不记名令牌身份验证的 REST api 。 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(); }

You most likely just need to add a HTTP header like this:您很可能只需要像这样添加 HTTP header :

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).澄清您在 POST 正文中发送的内容类型(假设您在 POST 正文发送 JSON - 否则根据需要进行调整)。 This has nothing to do with your bearer token authentication...这与您的不记名令牌身份验证无关......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM