大家晚上好。
我对 C# 有点陌生,但我正在玩 github api,我正试图了解一些基本的身份验证。我编写了一个控制台应用程序,其唯一目的是链接 github 站点并从那里拉下我的个人资料。当我进行不需要身份验证的呼叫时,它可以正常工作。
但是,如果我尝试使用基本身份验证进行身份验证,则会收到 401 错误,即使我输入了正确的用户名和密码。我用谷歌搜索,看起来我的代码是正确的,但我不知道为什么我没有被授权。
我附上了下面的代码,所有使用的东西都存在且正确。
class gitHubConnection : ConnectionManager
{
public override string getRepositories()
{
string web_response = null;
try
{
CredentialCache cache = new CredentialCache();
NetworkCredential nc = new NetworkCredential("githubUsername", "githubPassword");
cache.Add(new Uri("https://api.github.com/user"), "Basic", nc);
WebRequest request = (HttpWebRequest)WebRequest.Create("https://api.github.com/user");
request.Credentials = nc;
WebResponse response = request.GetResponse();
web_response = response.ToString();
}
catch (UriFormatException e)
{
Console.WriteLine("rats!!!!" + e.StackTrace);
}
catch (IOException ioe)
{
Console.WriteLine("something went pop " + ioe.StackTrace);
}
return web_response;
}
}
非常感谢您花时间看这个。我确定我忘记了什么,但我就是不知道是什么。
威尔士男孩