Please let me know if this is not possible…but in an effort to refactor my personal API I decided to start calling the Twitch endpoints through my API so data can be combined. To do this I direct the user to the auth page and get a bearer token back. I then pass that token to my API in the header. For some reason I get a 401 if I try to use that token at all from my API. I have no idea why as I can’t view a reason in the response. The token works from postman.
Here is an example of a request I make in my API:
public async Task<bool> ValidateToken()
{
var response = await client.GetAsync("https://id.twitch.tv/oauth2/validate");
return response.StatusCode == HttpStatusCode.OK;
}
The HttpClient is created as follows before the validation method is called:
public TwitchService(IHeaderDictionary headers)
{
StringValues token;
StringValues clientId;
var hasToken = headers.TryGetValue("Authorization", out token);
var hasClientId = headers.TryGetValue("Client-id", out clientId);
client = new HttpClient();
client.DefaultRequestHeaders.Add("Accept", "application/json");
if (hasToken)
{
var authToken = token.ToString().Replace("Bearer", "");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authToken);
}
if (hasClientId)
{
client.DefaultRequestHeaders.Add("Client-ID", clientId.ToString());
}
}
This may not be the right place to ask this. If so, let me know and I can move to StackOverflow.
Thanks for the formatting fix! Interesting. Ok that makes sense for that endpoint, but I have the same issue with the following helix endpoints:
public async Task GetCurrentUser()
{
var response = await client.GetAsync("https://api.twitch.tv/helix/users");
var test = 1;
}
public async Task<int> GetFollowerCount(string userId)
{
var response = await client.GetAsync($"http://api.twitch.tv/helix/users/follows?to_id={userId}");
return 0;
}
{“error”:“Unauthorized”,“status”:401,“message”:“OAuth token is missing”}
I’m confused though because I have confirmed that the header exists before making the request. I have also tried using both ‘Bearer’ and ‘OAuth’ as the scheme.
Yep I have debugged and the headers seem correct to me. I refactored a bit to new up a HttpClient specifically for a method call just to see what happens:
private void AddHeaders(HttpClient client)
{
StringValues token;
StringValues clientId;
var hasToken = headers.TryGetValue("Authorization", out token);
var hasClientId = headers.TryGetValue("Client-id", out clientId);
client.DefaultRequestHeaders.Add("Accept", "application/json");
if (hasToken)
{
var authToken = token.ToString().Replace("Bearer", "");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authToken);
}
if (hasClientId)
{
client.DefaultRequestHeaders.Add("Client-id", clientId.ToString());
}
}
public async Task<int> GetFollowerCount(string userId)
{
using (var client = new HttpClient())
{
AddHeaders(client);
var response = await client.GetAsync($"http://api.twitch.tv/helix/users/follows?to_id={userId}");
var test = await response.Content.ReadAsStringAsync();
}
return 0;
}
Here are the DefaultRequestHeaders that are added to the HttpClient before the request is made:
Seeing this now, I see that my auth header is not going across in the request even though it is added to my HttpClient. Very strange. So it is getting lost somehow but the clientId doesn’t.
EDIT:
I looked into this now that I realize the request was somehow missing the auth header. Apparently this is by design and the request has to be done again according to this stack overflow post and I can confirm that it works: