/// <summary>
        /// Creates a request token. This corresponds to the Hyves auth.requesttoken method.
        /// </summary>
        /// <param name="requestTokenSecret">The request token secret to use to sign the request.</param>
        /// <returns>A new requesttoken on success.</returns>
        public string CreateRequestToken(out string tokenSecret, HyvesExpirationType expirationType)
        {
            tokenSecret = string.Empty;
            string          token       = null;
            HttpWebResponse webResponse = null;

            HttpWebRequest webRequest = CreateRequest(null, null, expirationType, session);

            try
            {
                webResponse = (HttpWebResponse)webRequest.GetResponse();
            }
            catch (WebException we)
            {
                webResponse = (HttpWebResponse)we.Response;
            }

            HyvesResponse requestTokenResponse = new HyvesResponse(webResponse.GetResponseStream(), HyvesMethod.Unknown);

            if (requestTokenResponse.IsError == false)
            {
                Hashtable result = requestTokenResponse.Result as Hashtable;
                if (result != null)
                {
                    token = (string)result["oauth_token"];

                    tokenSecret = (string)result["oauth_token_secret"];
                }
            }

            return(token);
        }
        /// <summary>
        /// Starts an asynchronous call to create an authorization token. This corresponds to the
        /// Hyves auth.createToken method.
        /// </summary>
        /// <param name="callback">The async callback that is invoked when the request completes.</param>
        /// <param name="asyncState">The state to associate with the asynchronous call.</param>
        /// <returns>An async result that represents the asynchronous call.</returns>
        public IAsyncResult BeginCreateRequestToken(HyvesExpirationType expirationType, AsyncCallback callback, object asyncState)
        {
            if (this.asyncRequest != null)
            {
                throw new InvalidOperationException("A method is currently being invoked using this request.");
            }

            this.asyncRequest = CreateRequest(null, null, expirationType, this.session);
            return(this.asyncRequest.BeginGetResponse(callback, asyncState));
        }
    /// <summary>
    /// Starts an asynchronous call to create an authorization token. This corresponds to the
    /// Hyves auth.createToken method.
    /// </summary>
    /// <param name="callback">The async callback that is invoked when the request completes.</param>
    /// <param name="asyncState">The state to associate with the asynchronous call.</param>
    /// <returns>An async result that represents the asynchronous call.</returns>
    public IAsyncResult BeginCreateRequestToken(HyvesExpirationType expirationType, AsyncCallback callback, object asyncState)
    {
      if (this.asyncRequest != null)
      {
        throw new InvalidOperationException("A method is currently being invoked using this request.");
      }

      this.asyncRequest = CreateRequest(null, null, expirationType, this.session);
      return this.asyncRequest.BeginGetResponse(callback, asyncState);
    }
        private static HttpWebRequest CreateRequest(string requestToken, string requestTokenSecret, HyvesExpirationType expirationType, HyvesSession session)
        {
            HyvesRequestParameterList parameters = new HyvesRequestParameterList();
            HyvesMethod method;

            if (string.IsNullOrEmpty(requestToken))
            {
                method = HyvesMethod.AuthRequesttoken;

                StringBuilder methodsStringBuilder = new StringBuilder();

                if (session.Methods.Contains(HyvesMethod.All))
                {
                    Array hyvesMethodValues = Enum.GetValues(typeof(HyvesMethod));
                    foreach (HyvesMethod hyvesMethod in hyvesMethodValues)
                    {
                        if (hyvesMethod != HyvesMethod.Unknown)
                        {
                            methodsStringBuilder.Append(string.Format("{0},", EnumHelper.GetDescription(hyvesMethod)));
                        }
                    }

                    methodsStringBuilder = methodsStringBuilder.Replace(
                        string.Format("{0},", EnumHelper.GetDescription(HyvesMethod.All)),
                        string.Empty);
                }
                else
                {
                    foreach (HyvesMethod hyvesMethod in session.Methods)
                    {
                        methodsStringBuilder.Append(string.Format("{0},", EnumHelper.GetDescription(hyvesMethod)));
                    }
                }

                string methods = methodsStringBuilder.ToString();
                parameters["methods"] = methods.Substring(0, methods.Length - 1);

                parameters["expirationtype"] = EnumHelper.GetDescription(expirationType);
            }
            else
            {
                session.InitializeToken(requestToken, requestTokenSecret, DateTime.MinValue);
                method = HyvesMethod.AuthAccesstoken;
            }

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(HyvesHttpUri);

            parameters.InitializeRequest(request, method, session);

            return(request);
        }
    /// <summary>
    /// Creates a request token. This corresponds to the Hyves auth.requesttoken method.
    /// </summary>
    /// <param name="requestTokenSecret">The request token secret to use to sign the request.</param>
    /// <returns>A new requesttoken on success.</returns>
    public string CreateRequestToken(out string tokenSecret, HyvesExpirationType expirationType)
    {
      tokenSecret = string.Empty;
      string token = null;
      HttpWebResponse webResponse = null;

      HttpWebRequest webRequest = CreateRequest(null, null, expirationType, session);
      try
      {
        webResponse = (HttpWebResponse)webRequest.GetResponse();
      }
      catch (WebException we)
      {
        webResponse = (HttpWebResponse)we.Response;
      }

      HyvesResponse requestTokenResponse = new HyvesResponse(webResponse.GetResponseStream(), HyvesMethod.Unknown);

      if (requestTokenResponse.IsError == false)
      {
        Hashtable result = requestTokenResponse.Result as Hashtable;
        if (result != null)
        {
          token = (string)result["oauth_token"];

          tokenSecret = (string)result["oauth_token_secret"];
        }
      }

      return token;
    }
    private static HttpWebRequest CreateRequest(string requestToken, string requestTokenSecret, HyvesExpirationType expirationType, HyvesSession session)
    {
      HyvesRequestParameterList parameters = new HyvesRequestParameterList();
      HyvesMethod method;

      if (string.IsNullOrEmpty(requestToken))
      {
        method = HyvesMethod.AuthRequesttoken;

        StringBuilder methodsStringBuilder = new StringBuilder();

        if (session.Methods.Contains(HyvesMethod.All))
        {
          Array hyvesMethodValues = Enum.GetValues(typeof(HyvesMethod));
          foreach (HyvesMethod hyvesMethod in hyvesMethodValues)
          {
            if (hyvesMethod != HyvesMethod.Unknown)
            {
              methodsStringBuilder.Append(string.Format("{0},", EnumHelper.GetDescription(hyvesMethod)));
            }
          }

          methodsStringBuilder=methodsStringBuilder.Replace(
            string.Format("{0},", EnumHelper.GetDescription(HyvesMethod.All)),
            string.Empty);
        }
        else
        {
          foreach (HyvesMethod hyvesMethod in session.Methods)
          {
            methodsStringBuilder.Append(string.Format("{0},", EnumHelper.GetDescription(hyvesMethod)));
          }
        }

        string methods = methodsStringBuilder.ToString();
        parameters["methods"] = methods.Substring(0, methods.Length - 1);

        parameters["expirationtype"] = EnumHelper.GetDescription(expirationType);
      }
      else
      {
        session.InitializeToken(requestToken, requestTokenSecret, DateTime.MinValue);
        method = HyvesMethod.AuthAccesstoken;
      }

      HttpWebRequest request = (HttpWebRequest)WebRequest.Create(HyvesHttpUri);
      parameters.InitializeRequest(request, method, session);

      return request;
    }