コード例 #1
0
        private HttpWebRequest GenerateRequest(string contentType, string requestMethod)
        {
            var ts = UnixTime.ToUnixTime(DateTime.Now);
            //Create the needed OAuth Parameters.
            //Refer - http://oauth.net/core/1.0/#sig_base_example
            var param = new OAuthParameters()
            {
                ConsumerKey     = _consumerKey,
                SignatureMethod = SigningProvider.SignatureMethod,
                Version         = Constants.Version1_0,
                Nonce           = NonceProvider.GenerateNonce(ts),
                Timestamp       = ts.ToString(),
            };

            //Generate Signature Hash
            var signatureBase = SignatureBase.Create(requestMethod.ToUpper(), _serviceProviderUri, param);

            //Set Signature Hash as one of the OAuth Parameter
            param.Signature = SigningProvider.ComputeSignature(signatureBase, _consumerSecret, null);

            var httpWebRequest = (HttpWebRequest)WebRequest.Create(_serviceProviderUri);

            httpWebRequest.Method      = requestMethod;
            httpWebRequest.ContentType = contentType;
            httpWebRequest.Timeout     = RequestTimeOut;
            //Add the OAuth Parameters to Authorization Header of Request
            httpWebRequest.Headers.Add(Constants.AuthorizationHeaderParameter, param.ToHeaderFormat());
            return(httpWebRequest);
        }
コード例 #2
0
ファイル: OAuthRequest.cs プロジェクト: yhtsnda/oauth-dot-net
        /// <summary>
        ///
        /// </summary>
        /// <param name="requestUri"></param>
        /// <param name="authParameters"></param>
        /// <param name="httpMethod"></param>
        /// <param name="contentType"></param>
        /// <param name="bodyStream"></param>
        /// <returns></returns>
        protected virtual HttpWebRequest CreateRequest(Uri requestUri, OAuthParameters authParameters, string httpMethod, string contentType, System.IO.Stream bodyStream)
        {
            NameValueCollection requestSpecificParameters = new NameValueCollection(authParameters.AdditionalParameters);

            if (!this.Service.UseAuthorizationHeader)
            {
                ////The OAuth params need to be added either into the querystring or into the post body.
                requestSpecificParameters.Add(authParameters.OAuthRequestParams());
            }

            if (Constants.HttpPostUrlEncodedContentTypeRegex.IsMatch(contentType) && bodyStream == null)
            {
                ////All the requestSpecificParameters need to be encoded into the body bytes
                string body = Rfc3986.EncodeAndJoin(requestSpecificParameters);
                bodyStream = new MemoryStream(Encoding.ASCII.GetBytes(body));
            }
            else
            {
                ////They go into the querystring.
                string query = Rfc3986.EncodeAndJoin(requestSpecificParameters);

                if (!string.IsNullOrEmpty(query))
                {
                    UriBuilder mutableRequestUri = new UriBuilder(requestUri);
                    if (string.IsNullOrEmpty(mutableRequestUri.Query))
                    {
                        mutableRequestUri.Query = query;
                    }
                    else
                    {
                        mutableRequestUri.Query = mutableRequestUri.Query.Substring(1) + "&" + query;
                    }

                    requestUri = mutableRequestUri.Uri;
                }
            }

            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(requestUri);

            request.Method = httpMethod;

            if (this.Service.UseAuthorizationHeader)
            {
                request.Headers.Add(HttpRequestHeader.Authorization, authParameters.ToHeaderFormat());
            }

            if (!String.IsNullOrEmpty(contentType))
            {
                request.ContentType = contentType;

                if (bodyStream != null)
                {
                    if (bodyStream.CanSeek)
                    {
                        request.ContentLength = bodyStream.Length;
                    }

                    StreamCopier.CopyTo(bodyStream, request.GetRequestStream());
                }
            }

            return(request);
        }