Question

When trying to get data from the github apir i got a 403. I want to use basic authentication (username and password).

I also tried it without credentials cache and setting the credentials directly, same result.

What am I doing wrong?

    string url = "https://api.github.com/search/users?q=gentlehag"

    HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
    request.Method = "GET";
    request.Proxy = null;
    request.PreAuthenticate = true;

    var crds = new NetworkCredential(Username, Password); 
    CredentialCache myCredentialCache = new CredentialCache();
    myCredentialCache.Add(new Uri(url), "Basic", crds);
    request.Credentials = myCredentialCache;

    request.KeepAlive = false;
    request.Accept = "application/vnd.github.v" + Version + "+json";

    using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
    {
        using (StreamReader reader = new StreamReader(response.GetResponseStream()))
        {
            return reader.ReadToEnd();
        }

    }

I also tried to set the basic authentication in the hider like this

 request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(Username + ":" + Password)));

-> same result.

In the config i set the config below to not get the ProtocolViolationException

<system.net>
    <settings>
      <httpWebRequest useUnsafeHeaderParsing = "true"/>
    </settings>
 </system.net>
Was it helpful?

Solution

After a lot of playing around and digging into the octokit sourcecode i found the solution.

I've to set a useragent.

Adding

request.UserAgent = "myTestApp";

solved it for me. perhaps this may help someone else

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top