-1

问题是 ToBase64string 中的字符串“resulted”在调试时正在转换,但是当我在编码后使用 result 时,似乎在我想在编码后使用“resulted”的“var response =”中,它不基于调试模式进行编码。为什么?我错过了什么吗

   [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;
        }
4

2 回答 2

0

当您的 Acesstoken 返回额外数据时,例如: " {\"access_token\":\"uKW7HeksFXz5QE1sF6Kjmfda5Fxi\",\"token_type\":\"Bearer\"}\r\n" 然后使用 'substring' 来夹盘例如,它到 accesstoken 代码应该如下所示 -> ' 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;
                    }
于 2019-06-25T18:50:14.377 回答
0

您需要在请求中添加授权标头。您需要将Authorization标头设置为Bearer [acces_token]

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

注意Bearer词后面有一个空格。

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

于 2019-06-20T19:58:09.940 回答