C
C#2y ago
James213

❔ Unable to make a GET request

I get a 403 - "Forbidden" from this code. I have successfully tested (200) with Postman and a Python version. Here is the doc fyi https://docs.github.com/en/rest/gists/gists?apiVersion=2022-11-28#list-gists-for-the-authenticated-user
// Create an HttpClient object (pass into function)
HttpClient client = new();

...


public async Task<HttpResponseMessage> GetResponse(string apiKey, string url, HttpClient client)
{
/*
Contents
1. Headers for the request
2. Return the response from the API using the HttpClient object
*/

// 1. Headers for the request
Dictionary<string, string> headers = new()
{
{ "Accept", "application/vnd.github+json" },
{ "Authorization", $"Bearer {apiKey}" },
{ "X-GitHub-Api-Version", "2022-11-28" }
};

// 2. Return the response from the API using the HttpClient object
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, url);
foreach (var header in headers)
{
request.Headers.Add(header.Key, header.Value);
}

HttpResponseMessage response = await client.SendAsync(request);

return response;

}
// Create an HttpClient object (pass into function)
HttpClient client = new();

...


public async Task<HttpResponseMessage> GetResponse(string apiKey, string url, HttpClient client)
{
/*
Contents
1. Headers for the request
2. Return the response from the API using the HttpClient object
*/

// 1. Headers for the request
Dictionary<string, string> headers = new()
{
{ "Accept", "application/vnd.github+json" },
{ "Authorization", $"Bearer {apiKey}" },
{ "X-GitHub-Api-Version", "2022-11-28" }
};

// 2. Return the response from the API using the HttpClient object
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, url);
foreach (var header in headers)
{
request.Headers.Add(header.Key, header.Value);
}

HttpResponseMessage response = await client.SendAsync(request);

return response;

}
5 Replies
James213
James213OP2y ago
I have also tried a different approach. yet the same error:
public async Task<HttpResponseMessage> GetResponse(string apiKey, string url, HttpClient client)
{
/*
Contents
1. Headers for the request
2. Return the response from the API using the HttpClient object
*/

// 1. Headers for the request
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, url);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.github+json"));
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);
request.Headers.Add("X-GitHub-Api-Version", "2022-11-28");

// 2. Return the response from the API using the HttpClient object
HttpResponseMessage response = await client.SendAsync(request);

return response;
}
public async Task<HttpResponseMessage> GetResponse(string apiKey, string url, HttpClient client)
{
/*
Contents
1. Headers for the request
2. Return the response from the API using the HttpClient object
*/

// 1. Headers for the request
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, url);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.github+json"));
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);
request.Headers.Add("X-GitHub-Api-Version", "2022-11-28");

// 2. Return the response from the API using the HttpClient object
HttpResponseMessage response = await client.SendAsync(request);

return response;
}
HimmDawg
HimmDawg2y ago
I don't see a user-agent header in your code. You have to use one
All API requests MUST include a valid User-Agent header. Requests with no User-Agent header will be rejected. We request that you use your GitHub username, or the name of your application, for the User-Agent header value. This allows us to contact you if there are problems.
All API requests MUST include a valid User-Agent header. Requests with no User-Agent header will be rejected. We request that you use your GitHub username, or the name of your application, for the User-Agent header value. This allows us to contact you if there are problems.
James213
James213OP2y ago
I got 200/OK. I'm guessing my python version does it automatically? As with curl, "curl sends a valid User-Agent header by default."
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server