Example #1
0
        protected virtual bool DoGetAccessToken()
        {
            // Fire the OnBeforeGetAccessToken event
            PreAccessTokenRequestEventArgs preArgs = new PreAccessTokenRequestEventArgs(
                this.Service.AccessTokenUrl,
                this.Service.AccessTokenEndPoint.HttpMethod,
                this.RequestToken,
                this.RequestTokenVerifier);

            if (this.OnBeforeGetAccessToken != null)
            {
                this.OnBeforeGetAccessToken(this, preArgs);
            }

            // Create and sign the request
            OAuthParameters authParams = this.CreateOAuthParameters(null);

            authParams.Verifier = preArgs.Verifier;

            // We don't have a verifier so something has gone wrong in the process.
            if (string.IsNullOrEmpty(authParams.Verifier))
            {
                return(false);
            }

            this.SignParameters(preArgs.RequestUri, preArgs.HttpMethod, authParams, this.RequestToken);

            HttpWebRequest request = this.CreateRequest(
                preArgs.RequestUri,
                authParams,
                preArgs.HttpMethod,
                preArgs.HttpMethod == "POST" ? Constants.HttpPostUrlEncodedContentType : String.Empty,
                null);

            HttpWebResponse response           = null;
            OAuthParameters responseParameters = null;

            // Get the service provider response
            try
            {
                response = (HttpWebResponse)request.GetResponse();

                // Parse the parameters and re-throw any OAuthRequestException from the service provider
                responseParameters = OAuthParameters.Parse(response);
                OAuthRequestException.TryRethrow(responseParameters);
            }
            catch (WebException e)
            {
                // Parse the parameters and re-throw any OAuthRequestException from the service provider
                responseParameters = OAuthParameters.Parse(e.Response as HttpWebResponse);
                OAuthRequestException.TryRethrow(responseParameters);

                // If no OAuthRequestException, rethrow the WebException
                throw;
            }

            // Store the access token
            this.AccessToken = new OAuthToken(
                TokenType.Access,
                responseParameters.Token,
                responseParameters.TokenSecret,
                this.Service.Consumer);

            // Fire the OnReceiveAccessToken event
            AccessTokenReceivedEventArgs responseArgs = new AccessTokenReceivedEventArgs(
                this.RequestToken,
                this.AccessToken,
                responseParameters.AdditionalParameters);

            if (this.OnReceiveAccessToken != null)
            {
                this.OnReceiveAccessToken(this, responseArgs);
            }

            return(true);
        }
Example #2
0
        protected virtual bool DoGetAccessToken()
        {
            // Fire the OnBeforeGetAccessToken event
            PreAccessTokenRequestEventArgs preArgs = new PreAccessTokenRequestEventArgs(
                this.Service.AccessTokenUrl,
                this.Service.AccessTokenEndPoint.HttpMethod,                 
                this.RequestToken,
                this.RequestTokenVerifier);

            if (this.OnBeforeGetAccessToken != null)
                this.OnBeforeGetAccessToken(this, preArgs);

            // Create and sign the request
            OAuthParameters authParams = this.CreateOAuthParameters(null);
            authParams.Verifier = preArgs.Verifier;

            // We don't have a verifier so something has gone wrong in the process.                
            if (string.IsNullOrEmpty(authParams.Verifier))
                return false;

            this.SignParameters(preArgs.RequestUri, preArgs.HttpMethod, authParams, this.RequestToken);

            HttpWebRequest request = this.CreateRequest(
                preArgs.RequestUri,
                authParams,
                preArgs.HttpMethod,
                preArgs.HttpMethod == "POST" ? Constants.HttpPostUrlEncodedContentType : String.Empty,
                null);

            HttpWebResponse response = null;
            OAuthParameters responseParameters = null;

            // Get the service provider response
            try
            {
                response = (HttpWebResponse)request.GetResponse();

                // Parse the parameters and re-throw any OAuthRequestException from the service provider
                responseParameters = OAuthParameters.Parse(response);
                OAuthRequestException.TryRethrow(responseParameters);
            }
            catch (WebException e)
            {
                // Parse the parameters and re-throw any OAuthRequestException from the service provider
                responseParameters = OAuthParameters.Parse(e.Response as HttpWebResponse);
                OAuthRequestException.TryRethrow(responseParameters);

                // If no OAuthRequestException, rethrow the WebException
                throw;
            }

            // Store the access token
            this.AccessToken = new OAuthToken(
                TokenType.Access,
                responseParameters.Token,
                responseParameters.TokenSecret,
                this.Service.Consumer);

            // Fire the OnReceiveAccessToken event
            AccessTokenReceivedEventArgs responseArgs = new AccessTokenReceivedEventArgs(
                this.RequestToken,
                this.AccessToken,
                responseParameters.AdditionalParameters);

            if (this.OnReceiveAccessToken != null)
                this.OnReceiveAccessToken(this, responseArgs);

            return true;
        }