C#C
C#3y 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;

        }
Was this page helpful?