2

I am trying to make a small application to get results of GitHub API to a text file.First I am trying to get data to the console.I tried many ways as well referred many documents but I couldn't find a way to fix this issues .

https://api.github.com/users/user?client_id=8763c42f48201b31115f&client_secret=4708b9aea8e35878b9748a016198b81de24352a4 Request forbidden by administrative rules. Please make sure your request has a User-Agent header (http://developer.github.com/v3/#user-agent-required). Check https://developer.github.com for other possible causes

This is the sample code I used.Can any one help me to fix the issue about the User-Agent header

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            GetGameData().Wait();     
        }
        public static async Task<string> GetGameData()
        {

            var url = "https://api.github.com/users/user?client_id=8763c42f48201b31115f&client_secret=4708b9aea8e35878b9748a016198b81de24352a4";
            using (HttpClient client = new HttpClient())
            {
                    client.BaseAddress = new Uri(url);
                    Console.WriteLine(client.BaseAddress);
                    HttpResponseMessage response = await client.GetAsync(url);
                    string strResult = await response.Content.ReadAsStringAsync();
                    Console.WriteLine(strResult);
                    return strResult;
            } 
        }

    }
}
Janith Udara
  • 625
  • 1
  • 7
  • 15
  • 2
    `client.DefaultRequestHeaders.Add("User-Agent", @"Mozilla/5.0 (Windows NT 10; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0");` – Jimi Nov 09 '18 at 09:53
  • I added this to the code but still I get the same error – Janith Udara Nov 09 '18 at 10:02
  • Try adding: `ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;` as the first line of the `GetGameData()` method. – Jimi Nov 09 '18 at 10:16
  • Again I find the same Error . – Janith Udara Nov 09 '18 at 10:56
  • In the current state of code you're presenting - if that's your actual code - this is probably all you can try. You would need to add an `HttpClientHandler` and other missing parts. Also, that's not really the way to pass an Auth Token. Search SO for examples on how to use the HttpClient class for Token authentication in a `OAuth2` scenario. You'll find many. – Jimi Nov 09 '18 at 11:07
  • I add this `client.DefaultRequestHeaders.Add("User-Agent", @"Mozilla/5.0 (Windows NT 10; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0"); ` command again. I think before I placed this part in a wrong place now it works even with this part ` client.DefaultRequestHeaders.Add("User-Agent", "Other"); ` Thanks for the help – Janith Udara Nov 09 '18 at 11:21

1 Answers1

1

For using HttpClient, Jimi's comment worked for me:

client.DefaultRequestHeaders.Add("User-Agent", @"Mozilla/5.0 (Windows NT 10; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0");

This question's example uses HttpClient, but if you want to use WebClient, see this question: Setting the User-Agent header for a WebClient request

nedstark179
  • 546
  • 6
  • 12
  • 23