public virtual void Write(OAuthMessage message, System.IO.Stream requestStream)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }
            if (requestStream == null)
            {
                throw new ArgumentNullException("requestStream");
            }

            System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(requestStream);
            AccessTokenRequest     atRequestMsg = message as AccessTokenRequest;

            if (atRequestMsg != null)
            {
                streamWriter.Write(this.GetFormEncodedQueryFormat(message));
                streamWriter.Flush();
                return;
            }

            AccessTokenResponse atResponseMsg = message as AccessTokenResponse;

            if (atResponseMsg != null)
            {
                streamWriter.Write(this.GetJsonEncodedFormat(message));
                streamWriter.Flush();
                return;
            }

            throw new OAuthMessageException(string.Format(Resources.ID3724, message.GetType()));
        }
        protected virtual OAuthMessage CreateTypedOAuthMessageFromParameters(Uri baseUri, Dictionary <string, string> parameters)
        {
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }

            OAuthMessage request = null;

            if (parameters.ContainsKey("response_type") && (parameters["response_type"] == "code" || parameters["response_type"] == "token"))
            {
                request = new EndUserAuthorizationRequest(baseUri);
            }
            if ((parameters.ContainsKey("code") && !string.IsNullOrEmpty(parameters["code"])) || (parameters.ContainsKey("access_token") && !string.IsNullOrEmpty(parameters["access_token"]) && parameters.ContainsKey("refresh_token") && string.IsNullOrEmpty(parameters["refresh_token"])))
            {
                request = new EndUserAuthorizationResponse(baseUri);
            }
            if (parameters.ContainsKey("error") && !string.IsNullOrEmpty(parameters["error"]))
            {
                request = new EndUserAuthorizationFailedResponse(baseUri);
            }
            if (parameters.ContainsKey("grant_type") && !string.IsNullOrEmpty(parameters["grant_type"]) && parameters["grant_type"] == "authorization_code")
            {
                request = new AccessTokenRequestWithAuthorizationCode(baseUri);
            }
            if (parameters.ContainsKey("access_token") && !string.IsNullOrEmpty(parameters["access_token"]))
            {
                request = new AccessTokenResponse(baseUri);
            }
            if (request == null)
            {
                throw new OAuthMessageSerializationException(Resources.ID3723);
            }

            foreach (var parameter in parameters)
            {
                request.Parameters.Add(parameter.Key, parameter.Value);
            }
            request.Validate();
            return(request);
        }
        public virtual string GetHttpContentType(OAuthMessage message)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            AccessTokenRequest atRequestMessage = message as AccessTokenRequest;

            if (atRequestMessage != null)
            {
                return("application/x-www-form-urlencoded");
            }

            AccessTokenResponse atResponseMessage = message as AccessTokenResponse;

            if (atResponseMessage != null)
            {
                return("application/json");
            }
            return("text/plain; charset=us-ascii");
        }
        public virtual string GetHttpMethod(OAuthMessage message)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            AccessTokenRequest atRequestMessage = message as AccessTokenRequest;

            if (atRequestMessage != null)
            {
                return("POST");
            }

            AccessTokenResponse atResponseMessage = message as AccessTokenResponse;

            if (atResponseMessage != null)
            {
                return("POST");
            }

            return("GET");
        }
        protected virtual OAuthMessage CreateTypedOAuthMessageFromParameters(Uri baseUri, Dictionary<string,string> parameters)
        {
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }

            OAuthMessage request = null;
            if (parameters.ContainsKey("response_type") && (parameters["response_type"] == "code" || parameters["response_type"] == "token"))
            {
                request = new EndUserAuthorizationRequest(baseUri);
            }
            if ((parameters.ContainsKey("code")  && !string.IsNullOrEmpty(parameters["code"])) || (parameters.ContainsKey("access_token") && !string.IsNullOrEmpty(parameters["access_token"]) && parameters.ContainsKey("refresh_token")  && string.IsNullOrEmpty(parameters["refresh_token"])))
            {
                request = new EndUserAuthorizationResponse(baseUri);
            }
            if (parameters.ContainsKey("error")  && !string.IsNullOrEmpty(parameters["error"]))
            {
                request = new EndUserAuthorizationFailedResponse(baseUri);
            }
            if (parameters.ContainsKey("grant_type")  && !string.IsNullOrEmpty(parameters["grant_type"]) && parameters["grant_type"] == "authorization_code")
            {
                request = new AccessTokenRequestWithAuthorizationCode(baseUri);
            }
            if (parameters.ContainsKey("access_token")  && !string.IsNullOrEmpty(parameters["access_token"]))
            {
                request = new AccessTokenResponse(baseUri);
            }
            if (request == null)
            {
                throw new OAuthMessageSerializationException(Resources.ID3723);
            }

            foreach (var parameter in parameters)
            {
                request.Parameters.Add(parameter.Key, parameter.Value);
            }
            request.Validate();
            return request;
        }