Пример #1
0
		private bool DoGetAccessToken() {
			// Fire the OnBeforeGetAccessToken event
			PreAccessTokenRequestEventArgs preArgs = new PreAccessTokenRequestEventArgs(Service.AccessTokenUrl,
			                                                                            Service.AccessTokenEndPoint.HttpMethod,
			                                                                            RequestToken, RequestTokenVerifier);
			if (BeforeGetAccessToken != null)
				BeforeGetAccessToken(this, preArgs);

			// Create and sign the request
			OAuthParameters authParams = 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;

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

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

			OAuthParameters responseParameters;

			// Get the service provider response
			try {
				HttpWebResponse 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
			AccessToken = new OAuthToken(TokenType.Access, responseParameters.Token, responseParameters.TokenSecret, Service.Consumer);

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

			if (ReceiveAccessToken != null)
				ReceiveAccessToken(this, responseArgs);

			return true;
		}
Пример #2
0
        private bool DoGetAccessToken()
        {
            // Fire the OnBeforeGetAccessToken event
            PreAccessTokenRequestEventArgs preArgs = new PreAccessTokenRequestEventArgs(Service.AccessTokenUrl,
                                                                                        Service.AccessTokenEndPoint.HttpMethod,
                                                                                        RequestToken, RequestTokenVerifier);

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

            // Create and sign the request
            OAuthParameters authParams = 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);
            }

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

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

            OAuthParameters responseParameters;

            // Get the service provider response
            try {
                HttpWebResponse 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
            AccessToken = new OAuthToken(TokenType.Access, responseParameters.Token, responseParameters.TokenSecret, Service.Consumer);

            // Fire the OnReceiveAccessToken event
            AccessTokenReceivedEventArgs responseArgs = new AccessTokenReceivedEventArgs(RequestToken, AccessToken);

            responseArgs.AdditionalParameters.Add(responseParameters.AdditionalParameters);

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

            return(true);
        }