/// <summary>
        /// Removes security key, does not
        /// </summary>
        public void SignOut(bool keeyConsumerKey = false, bool deleteCache = true)
        {
            AuthResult = new TDAuthResult
            {
                consumer_key = keeyConsumerKey? AuthResult.consumer_key : null
            };

            if (deleteCache)
            {
                _cache.Save("TDAmeritradeKey", JsonConvert.SerializeObject(AuthResult));
            }

            IsSignedIn = false;
            OnSignedIn(false);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Signs in using cache authentication code
        /// </summary>
        /// <returns></returns>
        public async Task PostRefreshToken()
        {
            AuthResult = JsonSerializer.Deserialize <TDAuthResult>(_cache.Load("TDAmeritradeKey"));

            var decoded = HttpUtility.UrlDecode(AuthResult.security_code);

            var path = "https://api.tdameritrade.com/v1/oauth2/token";

            var dict = new Dictionary <string, string>
            {
                { "grant_type", "refresh_token" },
                { "access_type", "" },
                { "client_id", $"{AuthResult.consumer_key}@AMER.OAUTHAP" },
                { "redirect_uri", AuthResult.refresh_token },
                { "refresh_token", AuthResult.refresh_token },
                { "code", decoded }
            };

            var req = new HttpRequestMessage(HttpMethod.Post, path)
            {
                Content = new FormUrlEncodedContent(dict)
            };

            using (var client = new HttpClient())
            {
                var res = await client.SendAsync(req);

                switch (res.StatusCode)
                {
                case HttpStatusCode.OK:
                    var r = await res.Content.ReadAsStringAsync();

                    Console.WriteLine(r);
                    var result = JsonSerializer.Deserialize <TDAuthResult>(r);
                    AuthResult.access_token = result.access_token;
                    _cache.Save("TDAmeritradeKey", JsonSerializer.Serialize(AuthResult));
                    IsSignedIn     = true;
                    HasConsumerKey = true;
                    break;

                default:
                    Console.WriteLine("Error: " + res.ReasonPhrase);
                    break;
                }
            }
        }
        /// <summary>
        /// Sign in using code from SignInUrl
        /// The token endpoint returns an access token along with an optional refresh token.
        /// https://developer.tdameritrade.com/authentication/apis/post/token-0
        /// </summary>
        /// <param name="consumerKey">OAuth User ID of your application</param>
        /// <param name="code">Required if trying to use authorization code grant</param>
        /// <param name="redirectUrl">Required if trying to use authorization code grant</param>
        /// <returns></returns>
        public async Task SignIn(string consumerKey, string code, string redirectUrl = "http://localhost")
        {
            var decoded = HttpUtility.UrlDecode(code);
            var path    = "https://api.tdameritrade.com/v1/oauth2/token";

            using (var client = new HttpClient())
            {
                var dict = new Dictionary <string, string>
                {
                    { "grant_type", "authorization_code" },
                    { "access_type", "offline" },
                    { "client_id", $"{consumerKey}@AMER.OAUTHAP" },
                    { "redirect_uri", redirectUrl },
                    { "code", decoded }
                };

                var req = new HttpRequestMessage(HttpMethod.Post, path)
                {
                    Content = new FormUrlEncodedContent(dict)
                };
                var res = await client.SendAsync(req);

                switch (res.StatusCode)
                {
                case HttpStatusCode.OK:
                    var json = await res.Content.ReadAsStringAsync();

                    AuthResult = JsonConvert.DeserializeObject <TDAuthResult>(json);
                    AuthResult.security_code = code;
                    AuthResult.consumer_key  = consumerKey;
                    AuthResult.redirect_url  = redirectUrl;
                    _cache.Save("TDAmeritradeKey", JsonConvert.SerializeObject(AuthResult));
                    IsSignedIn     = true;
                    HasConsumerKey = true;
                    OnSignedIn(true);
                    break;

                default:
                    throw (new Exception($"{res.StatusCode} {res.ReasonPhrase}"));
                }
            }
        }