public override WebResponse ExecuteFeed(string feedURL, IProvider provider, Token connectionToken, TRANSPORT_METHOD transportMethod) { UriBuilder ub; /******** retrieve standard Fields ************/ ub = new UriBuilder(feedURL); ub.SetQueryparameter(AccessTokenQueryParameterKey, connectionToken.AccessToken); HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(ub.ToString()); request.Method = transportMethod.ToString(); //logger.LogContactsRequest(ub.ToString()); WebResponse wr; try { logger.Debug("Executing " + feedURL + " using " + transportMethod.ToString()); wr = (WebResponse)request.GetResponse(); logger.Info("Successfully executed " + feedURL + " using " + transportMethod.ToString()); } catch (Exception ex) { logger.Error(ErrorMessages.CustomFeedExecutionError(feedURL, null), ex); throw new OAuthException(ErrorMessages.CustomFeedExecutionError(feedURL, null), ex); } return(wr); }
public override WebResponse ExecuteFeed(string feedURL, IProvider provider, Token connectionToken, TRANSPORT_METHOD transportMethod, byte[] content = null, Dictionary <string, string> headers = null) { HttpWebRequest request; request = (HttpWebRequest)HttpWebRequest.Create(feedURL); request.ServicePoint.Expect100Continue = false; request.Method = transportMethod.ToString(); //if headers are specified, set/append them if (headers != null) { foreach (var header in headers) { switch (header.Key) { case "ContentLength": { request.ContentLength = long.Parse(header.Value); break; } case "ContentType": { request.ContentType = header.Value; break; } default: { request.Headers[header.Key] = header.Value; break; } } } } if (request.ContentLength == 0 & content.Length > 0) { request.ContentLength = content.Length; } request.GetRequestStream().Write(content, 0, content.Length); WebResponse wr = null; try { logger.Debug("Executing " + feedURL + " using " + transportMethod.ToString()); wr = (WebResponse)request.GetResponse(); logger.Info("Successfully executed " + feedURL + " using " + transportMethod.ToString()); } catch (Exception ex) { logger.Error(ErrorMessages.CustomFeedExecutionError(feedURL, null), ex); throw new OAuthException(ErrorMessages.CustomFeedExecutionError(feedURL, null), ex); } return(wr); }
public override WebResponse ExecuteFeed(string feedURL, IProvider provider, Token connectionToken, TRANSPORT_METHOD transportMethod) { string signature = ""; OAuthHelper oauthHelper = new OAuthHelper(); QueryParameters oauthParams = new QueryParameters(); oauthParams.Add("oauth_consumer_key", provider.Consumerkey); oauthParams.Add("oauth_nonce", oauthHelper.GenerateNonce()); oauthParams.Add("oauth_signature_method", provider.SignatureMethod.ToString()); oauthParams.Add("oauth_timestamp", oauthHelper.GenerateTimeStamp()); oauthParams.Add("oauth_token", connectionToken.AccessToken); oauthParams.Add("oauth_version", "1.0"); ////1. Generate Signature signature = oauthHelper.GenerateSignature(new Uri(feedURL), oauthParams, provider.Consumerkey, provider.Consumersecret, provider.SignatureMethod, TRANSPORT_METHOD.GET, connectionToken.TokenSecret); oauthParams.Add("oauth_signature", signature); //3.Connect and Execute Feed HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(feedURL); request.Method = transportMethod.ToString(); request.Headers.Add("Authorization", oauthHelper.GetAuthorizationHeader(oauthParams)); //request.ContentType = "application/atom+xml"; request.ContentLength = 0; WebResponse wr; try { logger.Debug("Executing " + feedURL + " using " + transportMethod.ToString()); wr = (WebResponse)request.GetResponse(); logger.Info("Successfully executed " + feedURL + " using " + transportMethod.ToString()); } catch (Exception ex) { logger.Error(ErrorMessages.CustomFeedExecutionError(feedURL, oauthParams), ex); throw new OAuthException(ErrorMessages.CustomFeedExecutionError(feedURL, oauthParams), ex); } return(wr); }
//public static WebResponse ExecuteFeed(string feedURL, TRANSPORT_METHOD transportMethod) //{ // UriBuilder ub; // /******** retrieve standard Fields ************/ // ub = new UriBuilder(feedURL); // //if (oauthParameters != null) // // foreach (var param in oauthParameters) // // ub.SetQueryparameter(param.Name, param.Value); // HttpWebRequest webRequest = null; // //StreamWriter requestWriter = null; // webRequest = System.Net.WebRequest.Create(feedURL) as HttpWebRequest; // webRequest.Method = transportMethod.ToString(); // webRequest.Timeout = 20000; // if (transportMethod == TRANSPORT_METHOD.POST) // { // webRequest.ServicePoint.Expect100Continue = false; // webRequest.ContentType = "application/x-www-form-urlencoded"; // //requestWriter = new StreamWriter(webRequest.GetRequestStream()); // //try // //{ // // requestWriter.Write(string.Empty); // //} // //catch // //{ // // throw; // //} // //finally // //{ // // requestWriter.Close(); // // requestWriter = null; // //} // } // WebResponse wr; // try // { // wr = (WebResponse)webRequest.GetResponse(); // } // catch (Exception ex) // { // throw new OAuthException("An Error occurred while executing " + feedURL + "!", ex); // } // return wr; //} public static WebResponse ExecuteFeed(string feedURL, TRANSPORT_METHOD transportMethod) { UriBuilder ub; /******** retrieve standard Fields ************/ ub = new UriBuilder(feedURL); //if (oauthParameters != null) // foreach (var param in oauthParameters) // ub.SetQueryparameter(param.Name, param.Value); HttpWebRequest webRequest = null; //StreamWriter requestWriter = null; webRequest = System.Net.WebRequest.Create(feedURL) as HttpWebRequest; webRequest.Method = transportMethod.ToString(); webRequest.Timeout = 20000; if (transportMethod == TRANSPORT_METHOD.POST) { webRequest.ServicePoint.Expect100Continue = false; webRequest.ContentType = "application/x-www-form-urlencoded"; //requestWriter = new StreamWriter(webRequest.GetRequestStream()); //try //{ // requestWriter.Write(string.Empty); //} //catch //{ // throw; //} //finally //{ // requestWriter.Close(); // requestWriter = null; //} } WebResponse wr; try { wr = (WebResponse)webRequest.GetResponse(); } catch (Exception ex) { throw new OAuthException("An Error occurred while executing " + feedURL + "!", ex); } return(wr); }
/// <summary> /// Generate Signature /// </summary> /// <param name="requestURL"></param> /// <param name="oauthParameters"></param> /// <param name="consumerKey"></param> /// <param name="consumerSecret"></param> /// <param name="signatureType"></param> /// <param name="httpMethod"></param> /// <param name="tokenSecret"></param> /// <returns></returns> public string GenerateSignature(Uri requestURL, QueryParameters oauthParameters, string consumerKey, string consumerSecret, SIGNATURE_TYPE signatureType, TRANSPORT_METHOD httpMethod, string tokenSecret) { QueryParameters tmpOauthParameters = new QueryParameters(); foreach (var param in oauthParameters) { if (param.Value != null && (param.Value.ToLower().Contains("http://") || param.Value.ToLower().Contains("https://"))) { tmpOauthParameters.Add(new QueryParameter(param.Key, Utility.UrlEncode(param.Value))); } else if (param.Value != null) { tmpOauthParameters.Add(new QueryParameter(param.Key, param.Value)); } } tmpOauthParameters[OAuthSignatureMethodKey] = ParseSignatureEnum(signatureType); string signature = ""; StringBuilder signatureBase = new StringBuilder(); //1. URL encode and process Request URL string normalizedRequestUrl; normalizedRequestUrl = string.Format("{0}://{1}", requestURL.Scheme, requestURL.Host); if (!((requestURL.Scheme == "http" && requestURL.Port == 80) || (requestURL.Scheme == "https" && requestURL.Port == 443))) { normalizedRequestUrl += ":" + requestURL.Port; } normalizedRequestUrl += requestURL.AbsolutePath; normalizedRequestUrl = Utility.UrlEncode(normalizedRequestUrl); //2. URL Encode callbackUrl (if present) //if (tmpOauthParameters.HasName(OAuthCallbackKey)) // tmpOauthParameters[OAuthCallbackKey] = Utility.UrlEncode(tmpOauthParameters[OAuthCallbackKey]); //tmpOauthParameters["scope"] = Utility.UrlEncode(tmpOauthParameters["scope"]); foreach (var p in Utility.GetQuerystringParameters(requestURL.ToString())) { tmpOauthParameters.Add(p.Key, UrlEncode(HttpUtility.UrlDecode(p.Value))); } //following works for Twitter with spaces //tmpOauthParameters.Add(p.Key, UrlEncode(HttpUtility.UrlDecode(p.Value))); //3. Perform Lexographic Sorting tmpOauthParameters.Sort(); //4. Generate Signature Base signatureBase.AppendFormat("{0}&", httpMethod.ToString().ToUpper()); signatureBase.AppendFormat("{0}&", normalizedRequestUrl); signatureBase.AppendFormat("{0}", Utility.UrlEncode(tmpOauthParameters.ToString())); string sbase = signatureBase.ToString(); logger.Debug("signature base:" + sbase); //5. Generate Signature switch (signatureType) { case SIGNATURE_TYPE.PLAINTEXT: { signature = Utility.UrlEncode(string.Format("{0}&{1}", consumerSecret, tokenSecret)); break; } case SIGNATURE_TYPE.HMACSHA1: { HMACSHA1 hmacsha1 = new HMACSHA1(); hmacsha1.Key = Encoding.ASCII.GetBytes(string.Format("{0}&{1}", UrlEncode(consumerSecret), string.IsNullOrEmpty(tokenSecret) ? "" : tokenSecret)); signature = GenerateSignatureUsingHash(sbase, hmacsha1); logger.Debug("HMACSHA1 signature:" + signature); break; } default: throw new ArgumentException("Unknown signature type", "signatureType"); } return(signature); }
//public static WebResponse ExecuteFeed(string feedURL, TRANSPORT_METHOD transportMethod) //{ // UriBuilder ub; // /******** retrieve standard Fields ************/ // ub = new UriBuilder(feedURL); // //if (oauthParameters != null) // // foreach (var param in oauthParameters) // // ub.SetQueryparameter(param.Name, param.Value); // HttpWebRequest webRequest = null; // //StreamWriter requestWriter = null; // webRequest = System.Net.WebRequest.Create(feedURL) as HttpWebRequest; // webRequest.Method = transportMethod.ToString(); // webRequest.Timeout = 20000; // if (transportMethod == TRANSPORT_METHOD.POST) // { // webRequest.ServicePoint.Expect100Continue = false; // webRequest.ContentType = "application/x-www-form-urlencoded"; // //requestWriter = new StreamWriter(webRequest.GetRequestStream()); // //try // //{ // // requestWriter.Write(string.Empty); // //} // //catch // //{ // // throw; // //} // //finally // //{ // // requestWriter.Close(); // // requestWriter = null; // //} // } // WebResponse wr; // try // { // wr = (WebResponse)webRequest.GetResponse(); // } // catch (Exception ex) // { // throw new OAuthException("An Error occurred while executing " + feedURL + "!", ex); // } // return wr; //} public static WebResponse ExecuteFeed(string feedURL, TRANSPORT_METHOD transportMethod) { UriBuilder ub; /******** retrieve standard Fields ************/ ub = new UriBuilder(feedURL); //if (oauthParameters != null) // foreach (var param in oauthParameters) // ub.SetQueryparameter(param.Name, param.Value); HttpWebRequest webRequest = null; //StreamWriter requestWriter = null; webRequest = System.Net.WebRequest.Create(feedURL) as HttpWebRequest; webRequest.Method = transportMethod.ToString(); webRequest.Timeout = 20000; if (transportMethod == TRANSPORT_METHOD.POST) { webRequest.ServicePoint.Expect100Continue = false; webRequest.ContentType = "application/x-www-form-urlencoded"; //requestWriter = new StreamWriter(webRequest.GetRequestStream()); //try //{ // requestWriter.Write(string.Empty); //} //catch //{ // throw; //} //finally //{ // requestWriter.Close(); // requestWriter = null; //} } WebResponse wr; try { wr = (WebResponse)webRequest.GetResponse(); } catch (Exception ex) { throw new OAuthException("An Error occurred while executing " + feedURL + "!", ex); } return wr; }
public override WebResponse ExecuteFeed(string feedURL, IProvider provider, Token connectionToken, TRANSPORT_METHOD transportMethod, byte[] content = null, Dictionary<string, string> headers = null) { HttpWebRequest request; request = (HttpWebRequest)HttpWebRequest.Create(feedURL); request.ServicePoint.Expect100Continue = false; request.Method = transportMethod.ToString(); //if headers are specified, set/append them if (headers != null) { foreach (var header in headers) { switch (header.Key) { case "ContentLength": { request.ContentLength = long.Parse(header.Value); break; } case "ContentType": { request.ContentType = header.Value; break; } default: { request.Headers[header.Key] = header.Value; break; } } } } if (request.ContentLength == 0 & content.Length > 0) request.ContentLength = content.Length; request.GetRequestStream().Write(content, 0, content.Length); WebResponse wr = null; try { logger.Debug("Executing " + feedURL + " using " + transportMethod.ToString()); wr = (WebResponse)request.GetResponse(); logger.Info("Successfully executed " + feedURL + " using " + transportMethod.ToString()); } catch (Exception ex) { logger.Error(ErrorMessages.CustomFeedExecutionError(feedURL, null), ex); throw new OAuthException(ErrorMessages.CustomFeedExecutionError(feedURL, null), ex); } return wr; }
public override WebResponse ExecuteFeed(string feedURL, IProvider provider, Token connectionToken, TRANSPORT_METHOD transportMethod) { UriBuilder ub; /******** retrieve standard Fields ************/ ub = new UriBuilder(feedURL); ub.SetQueryparameter(AccessTokenQueryParameterKey, connectionToken.AccessToken); HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(ub.ToString()); request.Method = transportMethod.ToString(); //logger.LogContactsRequest(ub.ToString()); WebResponse wr; try { logger.Debug("Executing " + feedURL + " using " + transportMethod.ToString()); wr = (WebResponse)request.GetResponse(); logger.Info("Successfully executed " + feedURL + " using " + transportMethod.ToString()); } catch (Exception ex) { logger.Error(ErrorMessages.CustomFeedExecutionError(feedURL, null), ex); throw new OAuthException(ErrorMessages.CustomFeedExecutionError(feedURL, null), ex); } return wr; }
public override WebResponse ExecuteFeed(string feedURL, IProvider provider, Token connectionToken, TRANSPORT_METHOD transportMethod) { string signature = ""; OAuthHelper oauthHelper = new OAuthHelper(); QueryParameters oauthParams = new QueryParameters(); oauthParams.Add("oauth_consumer_key", provider.Consumerkey); oauthParams.Add("oauth_nonce", oauthHelper.GenerateNonce()); oauthParams.Add("oauth_signature_method", provider.SignatureMethod.ToString()); oauthParams.Add("oauth_timestamp", oauthHelper.GenerateTimeStamp()); oauthParams.Add("oauth_token", connectionToken.AccessToken); oauthParams.Add("oauth_version", "1.0"); ////1. Generate Signature signature = oauthHelper.GenerateSignature(new Uri(feedURL), oauthParams, provider.Consumerkey, provider.Consumersecret, provider.SignatureMethod, TRANSPORT_METHOD.GET, connectionToken.TokenSecret); oauthParams.Add("oauth_signature", signature); //3.Connect and Execute Feed HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(feedURL); request.Method = transportMethod.ToString(); request.Headers.Add("Authorization", oauthHelper.GetAuthorizationHeader(oauthParams)); //request.ContentType = "application/atom+xml"; request.ContentLength = 0; WebResponse wr; try { logger.Debug("Executing " + feedURL + " using " + transportMethod.ToString()); wr = (WebResponse)request.GetResponse(); logger.Info("Successfully executed " + feedURL + " using " + transportMethod.ToString()); } catch (Exception ex) { logger.Error(ErrorMessages.CustomFeedExecutionError(feedURL, oauthParams), ex); throw new OAuthException(ErrorMessages.CustomFeedExecutionError(feedURL, oauthParams), ex); } return wr; }
/// <summary> /// Generate Signature /// </summary> /// <param name="requestURL"></param> /// <param name="oauthParameters"></param> /// <param name="consumerKey"></param> /// <param name="consumerSecret"></param> /// <param name="signatureType"></param> /// <param name="httpMethod"></param> /// <param name="tokenSecret"></param> /// <returns></returns> public string GenerateSignature(Uri requestURL, QueryParameters oauthParameters, string consumerKey, string consumerSecret, SIGNATURE_TYPE signatureType, TRANSPORT_METHOD httpMethod, string tokenSecret) { QueryParameters tmpOauthParameters = new QueryParameters(); foreach (var param in oauthParameters) { if (param.Value.ToLower().Contains("http://") || param.Value.ToLower().Contains("https://")) tmpOauthParameters.Add(new QueryParameter(param.Key, Utility.UrlEncode(param.Value))); else tmpOauthParameters.Add(new QueryParameter(param.Key, param.Value)); } tmpOauthParameters[OAuthSignatureMethodKey] = ParseSignatureEnum(signatureType); string signature = ""; StringBuilder signatureBase = new StringBuilder(); //1. URL encode and process Request URL string normalizedRequestUrl; normalizedRequestUrl = string.Format("{0}://{1}", requestURL.Scheme, requestURL.Host); if (!((requestURL.Scheme == "http" && requestURL.Port == 80) || (requestURL.Scheme == "https" && requestURL.Port == 443))) { normalizedRequestUrl += ":" + requestURL.Port; } normalizedRequestUrl += requestURL.AbsolutePath; normalizedRequestUrl = Utility.UrlEncode(normalizedRequestUrl); //2. URL Encode callbackUrl (if present) //if (tmpOauthParameters.HasName(OAuthCallbackKey)) // tmpOauthParameters[OAuthCallbackKey] = Utility.UrlEncode(tmpOauthParameters[OAuthCallbackKey]); //tmpOauthParameters["scope"] = Utility.UrlEncode(tmpOauthParameters["scope"]); foreach (var p in Utility.GetQuerystringParameters(requestURL.ToString())) tmpOauthParameters.Add(p.Key, UrlEncode(HttpUtility.UrlDecode(p.Value))); //following works for Twitter with spaces //tmpOauthParameters.Add(p.Key, UrlEncode(HttpUtility.UrlDecode(p.Value))); //3. Perform Lexographic Sorting tmpOauthParameters.Sort(); //4. Generate Signature Base signatureBase.AppendFormat("{0}&", httpMethod.ToString().ToUpper()); signatureBase.AppendFormat("{0}&", normalizedRequestUrl); signatureBase.AppendFormat("{0}", Utility.UrlEncode(tmpOauthParameters.ToString())); string sbase = signatureBase.ToString(); logger.Debug("signature base:" + sbase); //5. Generate Signature switch (signatureType) { case SIGNATURE_TYPE.PLAINTEXT: { signature = Utility.UrlEncode(string.Format("{0}&{1}", consumerSecret, tokenSecret)); break; } case SIGNATURE_TYPE.HMACSHA1: { HMACSHA1 hmacsha1 = new HMACSHA1(); hmacsha1.Key = Encoding.ASCII.GetBytes(string.Format("{0}&{1}", UrlEncode(consumerSecret), string.IsNullOrEmpty(tokenSecret) ? "" : tokenSecret)); signature = GenerateSignatureUsingHash(sbase, hmacsha1); logger.Debug("HMACSHA1 signature:" + signature); break; } default: throw new ArgumentException("Unknown signature type", "signatureType"); } return signature; }