简体   繁体   中英

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. 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]

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.

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 '

how? Accesstoken.Substring(x , x ); (x= some number your subtracting from string)

then put it in your 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;
                    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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