Example #1
0
        /// <summary>
        /// Revokes an OAuth access token.
        /// https://apisb.etrade.com/docs/api/authorization/renew_access_token.html
        /// </summary>
        /// <param name="requestTokenInfo"></param>
        /// <param name="verifier"></param>
        /// <returns>an access token</returns>
        protected async Task RevokeAccessTokenAsync()
        {
            try
            {
                var tokenParameters = new OAuthParameters()
                {
                    HttpMethod = HttpMethod.Get,
                    Url        = $"{GetServer(EServer.OAuth)}revoke_access_token",
                    Binding    = OAuthParametersBinding.Header,
                    Values     = new Dictionary <string, string>
                    {
                        { "oauth_callback", "oob" },
                        { "oauth_token", Credentials.AccessToken.oauth_token },
                        { "oauth_token_secret", Credentials.AccessToken.oauth_token_secret }
                    }
                };

                var accessTokenInfo = await OAuthSvc.RevokeAccessTokenAsync(tokenParameters);

                Credentials.AccessToken = null;
            }
            catch (Exception ex)
            {
                throw new Exception("RenewAccessTokenAsync failed: ", ex);
            }
        }
Example #2
0
        /// <summary>
        /// Returns an access token.
        /// https://apisb.etrade.com/docs/api/authorization/get_access_token.html
        /// </summary>
        /// <param name="requestTokenInfo"></param>
        /// <param name="verifier"></param>
        /// <returns>an access token</returns>
        protected async Task GetAccessTokenAsync(string oauth_verifier)
        {
            try
            {
                var tokenParameters = new OAuthParameters()
                {
                    HttpMethod = HttpMethod.Get,
                    Url        = $"{GetServer(EServer.OAuth)}access_token",
                    Binding    = OAuthParametersBinding.Header,
                    Values     = new Dictionary <string, string>
                    {
                        { "oauth_callback", "oob" },
                        { "oauth_token", Credentials.RequestToken.oauth_token },
                        { "oauth_token_secret", Credentials.RequestToken.oauth_token_secret },
                        { "oauth_verifier", oauth_verifier }
                    }
                };

                var tokenExpiresTime = GetTokenExpirationTime();
                var accessTokenInfo  = await OAuthSvc.GetAccessTokenAsync(tokenParameters);

                Credentials.AccessToken = new AccessTokenResponse()
                {
                    oauth_token        = accessTokenInfo.oauth_token,
                    oauth_token_secret = accessTokenInfo.oauth_token_secret,
                    tokenExpiresTime   = tokenExpiresTime
                };
            }
            catch (Exception ex)
            {
                throw new Exception("GetAccessTokenAsync failed: ", ex);
            }
        }
Example #3
0
        /// <summary>
        /// This API returns a temporary request token that begins the OAuth process.
        /// The request token must accompany the user to the authorization page, where the user will grant your application limited access to the account. The token expires after five minutes.
        /// https://apisb.etrade.com/docs/api/authorization/request_token.html
        /// </summary>
        protected async Task GetRequestTokenAsync()
        {
            try
            {
                var tokenParameters = new OAuthParameters()
                {
                    HttpMethod = HttpMethod.Get,
                    Url        = $"{GetServer(EServer.OAuth)}request_token",
                    Binding    = OAuthParametersBinding.Header,
                    Values     = new Dictionary <string, string>()
                    {
                        { "oauth_callback", "oob" }
                    }
                };

                var requestTokenInfo = await OAuthSvc.GetRequestTokenAsync(tokenParameters);

                Credentials.RequestToken = new RequestTokenResponse()
                {
                    oauth_token              = requestTokenInfo.oauth_token,
                    oauth_token_secret       = requestTokenInfo.oauth_token_secret,
                    oauth_callback_confirmed = requestTokenInfo.oauth_callback_confirmed
                };
            }
            catch (Exception ex)
            {
                throw new Exception("GetRequestTokenAsync failed: ", ex);
            }
        }
        /// <summary>
        /// OAuth - 6.2.2. Service Provider Authenticates the User and Obtains Consent |
        /// ETrade - https://apisb.etrade.com/docs/api/authorization/authorize.html
        /// </summary>
        /// <param name="requestTokenInfo"></param>
        /// <returns></returns>
        protected async Task <AuthorizeResponse> AuthorizeApplicationAsync()
        {
            return(await Task.Run(() =>
            {
                var parameters = new OAuthParameters()
                {
                    HttpMethod = HttpMethod.Get,
                    Url = GetServer(EServer.Authorize),
                    Binding = OAuthParametersBinding.QueryString,
                    Values = new Dictionary <string, string>()
                    {
                        { "oauth_token", Credentials.RequestToken.oauth_token }
                    }
                };

                using (WebDriver)
                {
                    try
                    {
                        // login to account
                        WebDriver.Navigate().GoToUrl(OAuthSvc.GetAuthorizationUrl(parameters));
                        FindDriverElement("input", name: "USER").SendKeys(Credentials.userName);
                        FindDriverElement("input", name: "PASSWORD").SendKeys(Credentials.password);
                        var whiteListCookie = new Cookie(Credentials.consumerCookie.Key, Credentials.consumerCookie.Value);
                        WebDriver.Manage().Cookies.AddCookie(whiteListCookie);
                        FindDriverElement("button", id: "logon_button").Submit();

                        // agree to terms
                        FindDriverElement("input", name: "submit", value: "Accept").Submit();

                        // get & return the authorization code
                        string authorizationCode = FindDriverElement("input").GetAttribute("value");
                        return new AuthorizeResponse()
                        {
                            oauth_verifier = authorizationCode
                        };
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("AuthorizeApplicationAsync failed: ", ex);
                    }
                }
            }));
        }