【发布时间】:2013-11-14 14:10:15
【问题描述】:
大家晚上好。
我对 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;
}
}
非常感谢您抽出宝贵时间查看此内容。我确定我忘记了什么,但我就是不知道是什么。
威尔士男孩
【问题讨论】:
-
如果你有兴趣,你的问题让我很好奇,所以我尝试了一些研究为什么你的代码不起作用。这导致我提出以下问题:stackoverflow.com/q/19764113/849741
标签: c# http authentication github-api