Beispiel #1
0
        async Task <bool> AcquireAccessTokenAsync(string code, string state)
        {
            var contentParams = new Dictionary <string, string>
            {
                { "client_id", GhOAuthConfiguration.ClientId },
                { "client_secret", GhOAuthConfiguration.ClientSecret },
                { "code", code },
                { "state", state }
            };

            string content = String.Join("&", contentParams.Select(p => String.Format("{0}={1}", p.Key, p.Value)));

            try
            {
                SetLastError(null);

                var response = await _gitHubClient.Connection.Post <OAuthTokenResponse>(
                    new Uri("https://github.com/login/oauth/access_token", UriKind.Absolute),
                    content,
                    "application/json",
                    "application/x-www-form-urlencoded").ConfigureAwait(false);

                // TODO: save the entire json token (expiration time / renewal)
                string token = response.Body.access_token;

                var storedTokenInfo = new TokenSecurityInfo()
                {
                    AccessToken = token
                };

                VaultManager.Save(VaultTokenInfoResourceKey,
                                  VaultDefaultUnusedUserName,
                                  JsonConvert.SerializeObject(storedTokenInfo));

                return(true);
            }
            catch (Exception ex)
            {
                SetLastError(ex);
            }

            return(false);
        }
        async Task<bool> AcquireAccessTokenAsync(string code, string state)
        {
            var contentParams = new Dictionary<string, string>
                                        {
                                        {"client_id", GhOAuthConfiguration.ClientId },
                                        {"client_secret", GhOAuthConfiguration.ClientSecret },
                                        {"code", code },
                                        {"state", state }
                                        };

            string content = String.Join("&", contentParams.Select(p => String.Format("{0}={1}", p.Key, p.Value)));

            try
            {
                SetLastError(null);

                var response = await _gitHubClient.Connection.Post<OAuthTokenResponse>(
                        new Uri("https://github.com/login/oauth/access_token", UriKind.Absolute),
                        content,
                        "application/json",
                        "application/x-www-form-urlencoded").ConfigureAwait(false);

                // TODO: save the entire json token (expiration time / renewal)
                string token = response.Body.access_token;

                var storedTokenInfo = new TokenSecurityInfo()
                {
                    AccessToken = token
                };

                VaultManager.Save(VaultTokenInfoResourceKey,
                    VaultDefaultUnusedUserName,
                    JsonConvert.SerializeObject(storedTokenInfo));

                return true;
            }
            catch (Exception ex)
            {
                SetLastError(ex);
            }

            return false;
        }