Exemplo n.º 1
0
        /// <summary>
        /// Submit a web request using oAUTH.
        /// </summary>
        /// <param name="method">
        /// GET or POST
        /// </param>
        /// <param name="url">
        /// The full url, including the query string.
        /// </param>
        /// <param name="postData">
        /// Data to post (query string format)
        /// </param>
        /// <returns>
        /// The web server response.
        /// </returns>
        public string OAuthWebRequest(AuthUtilities.Method method, string url, string postData)
        {
            string outUrl;
            string querystring;

            // Setup postData for signing.
            // Add the postData to the querystring.
            if (method == AuthUtilities.Method.POST)
            {
                if (postData.Length > 0)
                {
                    // Decode the parameters and re-encode using the oAuth UrlEncode method.
                    var qs = HttpUtility.ParseQueryString(postData);
                    postData = string.Empty;
                    foreach (var key in qs.AllKeys)
                    {
                        if (postData.Length > 0)
                        {
                            postData += "&";
                        }

                        qs[key]   = HttpUtility.UrlDecode(qs[key]);
                        qs[key]   = this.UrlEncode(qs[key]);
                        postData += "{0}={1}".FormatWith(key, qs[key]);
                    }

                    if (url.IndexOf("?") > 0)
                    {
                        url += "&";
                    }
                    else
                    {
                        url += "?";
                    }

                    url += postData;
                }
            }
            else if (method == AuthUtilities.Method.GET && !string.IsNullOrEmpty(postData))
            {
                url += "?{0}".FormatWith(postData);
            }

            var uri = new Uri(url);

            var nonce     = this.GenerateNonce();
            var timeStamp = this.GenerateTimeStamp();

            // Generate Signature
            var sig = this.GenerateSignature(
                uri,
                this.ConsumerKey,
                this.ConsumerSecret,
                this.Token,
                this.TokenSecret,
                this.CallBackUrl,
                method.ToString(),
                timeStamp,
                nonce,
                this.PIN,
                out outUrl,
                out querystring);

            querystring += "&oauth_signature={0}".FormatWith(HttpUtility.UrlEncode(sig));

            // Convert the querystring to postData
            if (method == AuthUtilities.Method.POST)
            {
                postData    = querystring;
                querystring = string.Empty;
            }

            if (querystring.Length > 0)
            {
                outUrl += "?";
            }

            var ret = AuthUtilities.WebRequest(method, "{0}{1}".FormatWith(outUrl, querystring), postData);

            return(ret);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Submit a web request using oAUTH.
        /// </summary>
        /// <param name="method">
        /// GET or POST
        /// </param>
        /// <param name="url">
        /// The full url, including the query string.
        /// </param>
        /// <param name="postData">
        /// Data to post (query string format)
        /// </param>
        /// <returns>
        /// The web server response.
        /// </returns>
        public string OAuthWebRequest(AuthUtilities.Method method, string url, string postData)
        {
            // Setup postData for signing.
            // Add the postData to the querystring.
            if (method == AuthUtilities.Method.POST)
            {
                if (postData.Length > 0)
                {
                    // Decode the parameters and re-encode using the oAuth UrlEncode method.
                    var qs = HttpUtility.ParseQueryString(postData);
                    postData = string.Empty;

                    qs.AllKeys.ForEach(
                        key =>
                    {
                        if (postData.Length > 0)
                        {
                            postData += "&";
                        }

                        qs[key]   = HttpUtility.UrlDecode(qs[key]);
                        qs[key]   = this.UrlEncode(qs[key]);
                        postData += $"{key}={qs[key]}";
                    });

                    if (url.IndexOf("?", StringComparison.Ordinal) > 0)
                    {
                        url += "&";
                    }
                    else
                    {
                        url += "?";
                    }

                    url += postData;
                }
            }
            else if (method == AuthUtilities.Method.GET && postData.IsSet())
            {
                url += $"?{postData}";
            }

            var uri = new Uri(url);

            var nonce     = this.GenerateNonce();
            var timeStamp = this.GenerateTimeStamp();

            // Generate Signature
            var sig = this.GenerateSignature(
                uri,
                this.ConsumerKey,
                this.ConsumerSecret,
                this.Token,
                this.TokenSecret,
                this.CallBackUrl,
                method.ToString(),
                timeStamp,
                nonce,
                this.PIN,
                out var outUrl,
                out var querystring);

            querystring += $"&oauth_signature={HttpUtility.UrlEncode(sig)}";

            // Convert the querystring to postData
            if (method == AuthUtilities.Method.POST)
            {
                postData    = querystring;
                querystring = string.Empty;
            }

            if (querystring.Length > 0)
            {
                outUrl += "?";
            }

            var ret = AuthUtilities.WebRequest(method, $"{outUrl}{querystring}", postData);

            return(ret);
        }