Example #1
0
        public JsonResult GetRepos()
        {
            StringBuilder strb = new StringBuilder();

            strb.AppendFormat("access_token={0}", OAuthTokenData.GetTokenCache(HttpContext.Request));
            strb.AppendFormat("&type={0}", "public");
            strb.AppendFormat("&sort={0}", "updated");

            HttpWebRequest request =
                (HttpWebRequest)WebRequest.Create("https://api.github.com/user/repos?" + strb);

            request.Method = "GET";
            request.Accept = "application/json";

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            if (response.StatusCode == HttpStatusCode.OK)
            {
                StreamReader reader       = new StreamReader(response.GetResponseStream());
                string       responseData = reader.ReadToEnd();
                reader.Close();

                JavaScriptSerializer serializer = new JavaScriptSerializer();
                GitHubRepoFullInfo[] repos      = serializer.Deserialize <GitHubRepoFullInfo[]>(responseData);
                return(Json(repos, JsonRequestBehavior.AllowGet));
            }

            throw new Exception("fail to list repo statusCode=" + response.StatusCode);
        }
Example #2
0
        public ActionResult TokenAuthorize(string code, string state)
        {
            if (!String.IsNullOrEmpty(code))
            {
                StringBuilder strb = new StringBuilder();
                strb.AppendFormat("client_id={0}", ClientIdentifier);
                strb.AppendFormat("&client_secret={0}", ClientSecret);
                strb.AppendFormat("&code={0}", code);
                strb.AppendFormat("&state={0}", state);

                string postData = strb.ToString();

                HttpWebRequest request =
                    (HttpWebRequest)WebRequest.Create("https://github.com/login/oauth/access_token");
                request.Method        = "POST";
                request.ContentLength = postData.Length;
                request.ContentType   = "application/x-www-form-urlencoded";
                request.Accept        = "application/json";
                request.Headers.Add(GetAuthorizationHeader());

                StreamWriter writer = new
                                      StreamWriter(request.GetRequestStream());
                writer.Write(postData);
                writer.Close();

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    StreamReader reader       = new StreamReader(response.GetResponseStream());
                    string       responseData = reader.ReadToEnd();
                    reader.Close();

                    JavaScriptSerializer serializer = new JavaScriptSerializer();
                    OAuthTokenData       token      = serializer.Deserialize <OAuthTokenData>(responseData);
                    OAuthTokenData.SetTokenCache(HttpContext.Request, HttpContext.Response, token.access_token);
                }
            }

            return(RedirectToAction("Index", "home"));
        }
Example #3
0
        public JsonResult GetHooks(string url)
        {
            StringBuilder strb = new StringBuilder();

            strb.AppendFormat("access_token={0}", OAuthTokenData.GetTokenCache(HttpContext.Request));

            HttpWebRequest request =
                (HttpWebRequest)WebRequest.Create(url + "/hooks?" + strb);

            request.Method = "GET";
            request.Accept = "application/json";

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            if (response.StatusCode == HttpStatusCode.OK)
            {
                StreamReader reader       = new StreamReader(response.GetResponseStream());
                string       responseData = reader.ReadToEnd();
                reader.Close();
                return(Json(responseData, JsonRequestBehavior.AllowGet));
            }

            throw new Exception("fail to list hook statusCode=" + response.StatusCode);
        }
Example #4
0
 public JsonResult GetAccessToken()
 {
     return(Json(OAuthTokenData.GetTokenCache(HttpContext.Request), JsonRequestBehavior.AllowGet));
 }