Esempio n. 1
0
        private static OAuthResponse ParseCodeResponse(string s)
        {
            var obj    = new OAuthResponse();
            var groups = s.Split('&');

            obj.AccessToken = groups[0].Split('=')[1];
            obj.TokenType   = groups[2].Split('=')[1];

            return(obj);
        }
Esempio n. 2
0
        public static OAuthResponse ExchangeCode(string code)
        {
            if (!String.IsNullOrEmpty(code) && response == null)
            {
                try
                {
                    using (var web = new HttpClient())
                    {
                        var response = web.PostAsync($"https://github.com/login/oauth/access_token", new StringContent(Stringfy(new GitHubOAuthRequest()
                        {
                            client_id = client_id, client_secret = secret, redirect_uri = redirect_uri, code = GetCode(code)
                        }), System.Text.Encoding.UTF8, "application/json")).Result;
                        if (response.IsSuccessStatusCode)
                        {
                            var content = response.Content.ReadAsStringAsync().Result;
                            GitHubOAuthHelpers.response = ParseCodeResponse(content);

                            OnAuthChanged?.Invoke(true);

                            return(ParseCodeResponse(content));
                        }
                        throw new Exception("Bad Request");
                    }
                }
                catch (Exception ex)
                {
                    OnAuthChanged?.Invoke(false);
                    return(null);
                }
            }
            else
            {
                OnAuthChanged?.Invoke(false);
                return(null);
            }
        }