我正在尝试在Unity(.NET 3.5)中获取公共存储库的版本,但我一直收到403错误:
WebException: The remote server returned an error: (403) Forbidden.
System.Net.HttpWebRequest.CheckFinalStatus (System.Net.WebAsyncResult result)
System.Net.HttpWebRequest.SetResponseData (System.Net.WebConnectionData data)
这是我的代码:
private string GetReleases(string username, string repoName)
{
const string GITHUB_API = "https://api.github.com/repos/{0}/{1}/releases";
WebClient webClient = new WebClient();
Uri uri = new Uri(string.Format(GITHUB_API, username, repoName));
string releases = webClient.DownloadString(uri);
return releases;
}
因为repo是公开的,我相信我不需要使用令牌,但是为了防止我尝试添加一个令牌,我仍然得到相同的错误消息。
webClient.Headers.Add("Authorization", string.Format("Token {0}", "myToken"));
如果我从浏览器进入该网址,则会返回一个包含所有版本的json的json。
我试图获取版本时是否存在模板或错误?
答案 0 :(得分:2)
跟着梅森的comment,我能够让它工作,现在它返回了json!
private string GetReleases(string username, string repoName)
{
const string GITHUB_API = "https://api.github.com/repos/{0}/{1}/releases";
WebClient webClient = new WebClient();
// Added user agent
webClient.Headers.Add("User-Agent", "Unity web player");
Uri uri = new Uri(string.Format(GITHUB_API, username, repoName));
string releases = webClient.DownloadString(uri);
return releases;
}