Example #1
0
        public async Task <bool> Login()
        {
            var url      = "https://github.com/login/oauth/authorize?client_id=" + Uri.EscapeDataString(ClientId) + "&redirect_uri=" + Uri.EscapeDataString(RedirectUri) + "&scope=repo&display=popup&response_type=token";
            var startUri = new Uri(url);
            var endUri   = new Uri(RedirectUri);
            var webAuthenticationResult = await WebAuthenticationBroker.AuthenticateAsync(WebAuthenticationOptions.None, startUri, endUri);

            string code = "";

            switch (webAuthenticationResult.ResponseStatus)
            {
            case WebAuthenticationStatus.Success:
                var decoder = new WwwFormUrlDecoder(webAuthenticationResult.ResponseData.Replace(RedirectUri + "/?", ""));
                code = decoder.First(x => x.Name == "code").Value;
                break;

            case WebAuthenticationStatus.ErrorHttp:
                return(false);

                break;

            default:
                return(false);

                break;
            }
            var c    = new HttpClient();
            var data = new Dictionary <string, string>
            {
                { "client_id", ClientId },
                { "client_secret", ClientSecret },
                { "code", code }
            };
            var content = new FormUrlEncodedContent(data);
            var request = await c.PostAsync(Accesstokenuri, content);

            var result = await request.Content.ReadAsStringAsync();

            _accessToken = new WwwFormUrlDecoder(result).GetFirstValueByName("access_token");
            return(true);
        }