Github Api基本身份验证尝试获取数据时出错403

时间:2014-04-24 16:14:24

标签: basic-authentication github-api

当试图从github apir获取数据时,我得到了403。 我想使用基本身份验证(用户名和密码)。

我也尝试过没有凭据缓存并设置 凭证直接,结果相同。

我做错了什么?

    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();
        }

    }

我还尝试在hider中设置基本身份验证,如下所示

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

- >同样的结果。

在配置中,我将下面的配置设置为不获取ProtocolViolationException

<system.net>
    <settings>
      <httpWebRequest useUnsafeHeaderParsing = "true"/>
    </settings>
 </system.net>

1 个答案:

答案 0 :(得分:8)

经过大量的游戏并深入挖掘octokit源代码后,我找到了解决方案。

我要设置一个使用者。

添加

request.UserAgent = "myTestApp";

为我解决了。也许这可以帮助其他人