Jallrich
Jallrich

Reputation: 469

Get releases Github API v3

I'm trying to fetch the releases of a public repo in Unity (.NET 3.5) but I keep getting 403 errors:

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)

This is my code:

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

Because the repo is public I believe I don't need to use a token, but just in case I tried adding one and I still got the same error message.

webClient.Headers.Add("Authorization", string.Format("Token {0}", "myToken"));

If I enter from my browser to that url it returns me a json with all the releases of the repo.

Is there a template or something wrong with how I'm trying to get the releases?

Upvotes: 1

Views: 867

Answers (1)

Jallrich
Jallrich

Reputation: 469

Following mason's comment, I was able to make it work, now it returns the 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;
    }

Upvotes: 2

Related Questions