Skip to content
This repository has been archived by the owner on Oct 8, 2023. It is now read-only.

Using PAT to access Rest service #11

Closed
onterra opened this issue Feb 16, 2018 · 2 comments
Closed

Using PAT to access Rest service #11

onterra opened this issue Feb 16, 2018 · 2 comments

Comments

@onterra
Copy link

onterra commented Feb 16, 2018

Below results in html login response rather than json results.
Is this the correct use of PAT for Rest Access?

// try PAT Personal Access Token
var personalaccesstoken = "";

using (HttpClient client = new HttpClient())
{
	client.DefaultRequestHeaders.Accept.Add(
		new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("text/plain"));

	client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
		Convert.ToBase64String(
			System.Text.ASCIIEncoding.ASCII.GetBytes(
				string.Format("{0}:{1}", "", personalaccesstoken))));

	using (HttpResponseMessage message = client.GetAsync(baseAddress + restQuery).Result)
	{
		message.EnsureSuccessStatusCode();
		response.result = await message.Content.ReadAsStringAsync();
	}
}

thanks

@momohs
Copy link
Member

momohs commented Feb 16, 2018

To add the token to the request, you need to send it using the "bearer" scheme as follows:

client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", personalaccesstoken);

@momohs
Copy link
Member

momohs commented Feb 20, 2018

I'll close this issue for now. Please feel free to reopen it if you still have any problem.

@momohs momohs closed this as completed Feb 20, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants