Pxaml
Pxaml

Reputation: 555

pass a token to a GET Request xamarin forms

not sure if I am doing this right , passing a Token value and then get some info from a webservice. I edited this question , it is passing authentication. I will leave it for future searches.

 private async  void Data(string AUTH)
        {
            using (HttpClient client = new HttpClient())
            {


 var Tokens = Storage.access.AUTH;  
            var json = JsonConvert.SerializeObject(AUTH);

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AUTH);
            client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");
            var response = client.GetAsync("https://any adrees.com").Result;

            string content = response.Content.ReadAsStringAsync().Result;

            if (response.IsSuccessStatusCode)
            {

                var content2 = await response.Content.ReadAsStringAsync();

                var Items = JsonConvert.DeserializeObject<Mensajes>(content2);



            }
            Debug.WriteLine(content);

        }
    }

Upvotes: 2

Views: 5667

Answers (1)

lowleetak
lowleetak

Reputation: 1382

Not sure what type of token you are refer to, my answer will be based on OAuth access token.

You will need to create a AuthenticationHeaderValue and set it into HttpClient's headers.

var authHeader = new AuthenticationHeaderValue("bearer", Storage.accessToken.Token);
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Authorization = authHeader;

Upvotes: 4

Related Questions