2012-07-13 73 views
3

我正在使用GitHub API v3與C#。我能夠獲得訪問令牌,並使用它可以獲取用戶信息和回購信息。我如何發佈到GitHub API v3

但是,當我嘗試創建一個新的回購協議,我得到的錯誤爲未經授權。

我正在使用HttpWebRequest發佈數據,這可以在下面看到。請給我一些C#示例或示例代碼。

(..)string[] paramName, string[] paramVal, string json, string accessToken) 
{ 
    HttpWebRequest req = WebRequest.Create(new Uri(url)) as HttpWebRequest; 
    req.Method = "POST"; 
    req.ContentType = "application/json"; 

    StreamWriter writer = new StreamWriter(req.GetRequestStream()); 
    writer.Write(json); 
    writer.Close(); 

    string result = null; 
    using (HttpWebResponse resp = req.GetResponse() as HttpWebResponse) 
    { 
     StreamReader reader = 
      new StreamReader(resp.GetResponseStream()); 
     result = reader.ReadToEnd(); 
    }(..) 

注意:我不確定在哪裏需要添加accesstoken。我已經嘗試在標題以及在網址中,但他們都沒有工作。

+0

你找到一個解決這個問題?我遇到了同樣的問題。 – Gero 2013-09-23 09:54:24

+0

請注意,你現在可能只想使用它:https://github.com/octokit/octokit.net或者如果你不想全部複製你做的部分 - 這是一個公共github回購 – RhysC 2014-07-08 08:26:40

回答

2

你使用的是C# Github API example code?我會查看該代碼,看看它是否滿足您的需求。

+0

沒有。我沒有使用它。它的「非常早期的發展階段,只有認證在這個時候完成」。 – 2012-07-16 16:50:42

3

您可以只添加auth的請求頭很容易地使用基本身份驗證:

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

對不起還沒有使用的訪問令牌的東西呢。

1

這裏需要添加此令牌:

req.UserAgent = "My App"; 
req.Headers.Add("Authorization", string.Format("Token {0}", "..token..");