简体   繁体   English

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

[英]TObase64String not encoding my Access token , resulting to 401 unauthorized

The problem is the String 'resulted' in the ToBase64string is converting as it look like while debugging, but then when I use resulted after encoding it, seems like in the 'var response =' where I want to use 'resulted' after encode, it does not encode based in debug mode.问题是 ToBase64string 中的 String 'resulted' 正在按照调试时的样子进行转换,但是当我在编码后使用 result 时,似乎在'var response =' 中我想在编码后使用'resulted',它不基于调试模式进行编码。 why?为什么? Am I missing something我是不是错过了什么

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

You need to add authorization header into the request.您需要在请求中添加授权标头。 You need to set Authorization header to Bearer [acces_token]您需要将Authorization标头设置为Bearer [acces_token]

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

Notice there is a space after Bearer word.注意承载字后有一个空格。

This value new AuthenticationHeaderValue("Bearer "+ access_token) should be something like new AuthenticationHeaderValue("Bearer asadasda23qdaddfs45345") where asadasda23qdaddfs45345 is your access token you received.这个值new AuthenticationHeaderValue("Bearer "+ access_token)应该类似于new AuthenticationHeaderValue("Bearer asadasda23qdaddfs45345") ,其中asadasda23qdaddfs45345是您收到的访问令牌。

When your Acesstoken comes back with extra data for example: " {\\"access_token\\":\\"uKW7HeksFXz5QE1sF6Kjmfda5Fxi\\",\\"token_type\\":\\"Bearer\\"}\\r\\n" then use 'substring' to just chuck it down to the accesstoken code for example should look like this -> ' uKW7HeksFXz5QE1sF6Kjmfda5Fxi '当您的 Acesstoken 返回额外数据时,例如:" {\\"access_token\\":\\"uKW7HeksFXz5QE1sF6Kjmfda5Fxi\\",\\"token_type\\":\\"Bearer\\"}\\r\\n"然后使用 'substring' 来卡住例如,它到访问令牌代码应该如下所示 -> ' uKW7HeksFXz5QE1sF6Kjmfda5Fxi '

how?如何? Accesstoken.Substring(x , x ); Accesstoken.Substring(x, x); (x= some number your subtracting from string) (x=你从字符串中减去的某个数字)

then put it in your DefaultRequestHeaders然后把它放在你的 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;
                    }

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

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