【问题标题】:Even with a successful access Token I get a 401 unauthorized in my Get() async method?即使使用成功的访问令牌,我在 Get() 异步方法中也得到了 401 未授权?
【发布时间】:2019-10-26 05:50:41
【问题描述】:

目前我必须异步方法 Post() 和 Get()。因为我的 Post() 方法正在返回一个访问令牌,如果您查看我的 post 方法的底部,我也在其中调用我的 Get(),原因很简单,因为能够在我的 get 中调用字符串结果。但即使使用成功的访问令牌,我仍然会收到 401 未经授权的状态码,为什么?
Click to view error in VS

 namespace APICredential.Controllers
    {
        [RoutePrefix("api")]
        public class ValuesController : ApiController
        {

            [HttpGet, Route("values")]
            public async Task<string> Post()
            {
                using (HttpClient client = new HttpClient())
                {
                    client.BaseAddress = new Uri("https://api.elliemae.com/oauth2/");

                    var parameters = new Dictionary<string, string>()
                    {
                        {"grant_type", "password"}, //Gran_type Identified here
                        {"username", "admin@encompass:BE11200822"},
                        {"password", "Shmmar****"},
                        {"client_id", "gpq4sdh"},
                        {"client_secret", "dcZ42Ps0lyU0XRgpDyg0yXxxXVm9@A5Z4ICK3NUN&DgzR7G2tCOW6VC#HVoZPBwU"},
                        {"scope", "lp"}
                    };

                    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "v1/token")

                    {
                        Content = new FormUrlEncodedContent(parameters)
                    };

                    HttpResponseMessage response = await client.SendAsync(request);

                    string resulted = await response.Content.ReadAsStringAsync();
                   await Get(resulted);
                    return resulted;
                }
            }


            [HttpGet, Route("values/get")]
            public async Task<string> Get(string resulted)
            {

                string res = "";
                using (var client = new HttpClient())
                {
                    // HTTP POST

                    client.BaseAddress = new Uri("https://api.elliemae.com/");             
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    var response = client.GetAsync("/encompass/v1/loans/{ea7c29a6-ee08-4816-99d2-fbcc7d15731d}?Authorization=Bearer "+resulted+"&Content-Type=application/json").Result;

                    using (HttpContent content = response.Content)
                    {
                        // ... Read the string.
                        Task<string> result = content.ReadAsStringAsync();
                        res = result.Result;
                    }
                }
                return res;
            }

【问题讨论】:

  • 问题不够清楚。能改一下吗?
  • 确定一秒钟让我尽力而为
  • 希望有帮助,如果没有.. 我现在只想返回值,经过几个小时后我终于得到了我的访问令牌,但现在我想要诸如借款人姓名、电子邮件之类的值等等。
  • 问同样不准确的问题不会给你答案。你需要更多地描述你的问题到底是什么。您在邮递员中显示的值不包含在您的访问令牌中。

标签: c# asynchronous async-await access-token unauthorized


【解决方案1】:

缺少以下内容:

client.DefaultRequestHeaders.Add("Authorization", "Bearer " + Accesstoken);

这是您插入后的默认标题,然后您有权从您的 URL 中获取字符串数据...

代码将如下所示:

 public async Task<string> Get(string Accesstoken)
    {
         string res = "";
         using (var client = new HttpClient())
        {
            Accesstoken = Accesstoken.Substring(17, 28);
            client.BaseAddress = new Uri("https://api.elliemae.com/");
            client.DefaultRequestHeaders.Add("Authorization", "Bearer " + Accesstoken);
            var response = client.GetAsync("encompass/v1/loans/ea7c29a6-ee08-4816-99d2-fbcc7d15731d").Result;
            using (HttpContent content = response.Content)
            {
                // ... Read the string.
                Task<string> result = content.ReadAsStringAsync();
                res = result.Result;
            }

【讨论】:

    猜你喜欢
    • 2020-08-07
    • 2021-02-04
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多