TObase64String未对我的访问令牌进行编码,导致401未经授权



问题是 ToBase64string 中的字符串"结果"正在转换,就像调试时的样子一样,但是当我在编码后使用 result 时,似乎在编码后使用"var 响应 =",我想在编码后使用"结果",它不会基于调试模式进行编码。 为什么?我错过了什么吗

   [HttpGet, Route("values/get")]
        public async Task<string> Get(string resulted)
        {
            //resulted.Remove(0, 17);
             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"));
                client.DefaultRequestHeaders.Add("Authorization", "Bearer " + Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(resulted)));
                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;
        }

您需要将授权标头添加到请求中。您需要将授权标头设置为持有者 [acces_token]

httpClient.DefaultRequestHeaders.Authorization =
       new AuthenticationHeaderValue("Bearer "+ access_token);

请注意,持有者字后面有一个空格。

这个值 new AuthenticationHeaderValue("Bearer ">

+ access_token( 应该类似于 new AuthenticationHeaderValue("Bearer asadasda23qdaddfs45345"(,其中 asadasda23qdaddfs45345 是您收到的访问令牌。

当您的 Acesstoken 返回额外数据时,例如:"{\"access_token\":\">uKW7HeksFXz5QE1sF6Kjmfda5Fxi\",\"token_type\":\"Bearer\"}\r">然后使用"子字符串"将其压缩到访问令牌代码,例如应该看起来像这样 -> 'uKW7HeksFXz5QE1sF6Kjmfda5Fxi'

如何? Accesstoken.Substring(x , x (;(x=你从字符串中减去的一些数字(

然后把它放在你的 DefaultRequestHeaders 中

  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.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
                    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;
                    }

相关内容

  • 没有找到相关文章

最新更新