예제 #1
0
        public IEnumerable <IOAuthQueryParameter> GenerateApplicationParameters(
            IReadOnlyConsumerCredentials temporaryCredentials,
            IAuthenticationRequest authRequest = null,
            IEnumerable <IOAuthQueryParameter> additionalParameters = null)
        {
            var headers = GenerateConsumerParameters(temporaryCredentials).ToList();

            // Add Header for authenticated connection to a Twitter Application
            if (authRequest != null &&
                !string.IsNullOrEmpty(authRequest.AuthorizationKey) &&
                !string.IsNullOrEmpty(authRequest.AuthorizationSecret))
            {
                headers.Add(new OAuthQueryParameter("oauth_token", StringFormater.UrlEncode(authRequest.AuthorizationKey), true, true, false));
                headers.Add(new OAuthQueryParameter("oauth_token_secret", StringFormater.UrlEncode(authRequest.AuthorizationSecret), false, false, true));
            }
            else
            {
                headers.Add(new OAuthQueryParameter("oauth_token", "", false, false, true));
            }

            if (additionalParameters != null)
            {
                headers.AddRange(additionalParameters);
            }

            return(headers);
        }
예제 #2
0
        private static string CreateOAuthSecretKey(IEnumerable <IOAuthQueryParameter> oAuthQueryParameters)
        {
            var oAuthSecretKeyHeaders = oAuthQueryParameters.Where(x => x.IsPartOfOAuthSecretKey)
                                        .OrderBy(x => x.Key)
                                        .Select(x => StringFormater.UrlEncode(x.Value));

            return(string.Join("&", oAuthSecretKeyHeaders));
        }
예제 #3
0
        private static string CreateOAuthRequest(Uri uri, HttpMethod httpMethod, string urlParametersFormatted)
        {
            var url               = uri.Query == "" ? uri.AbsoluteUri : uri.AbsoluteUri.Replace(uri.Query, "");
            var encodedUrl        = StringFormater.UrlEncode(url);
            var encodedParameters = StringFormater.UrlEncode(urlParametersFormatted);

            return($"{httpMethod}&{encodedUrl}&{encodedParameters}");
        }
예제 #4
0
        public static string GetBearerTokenAuthorizationHeader(IReadOnlyConsumerCredentials credentials)
        {
            var concatenatedCredentials = StringFormater.UrlEncode(credentials.ConsumerKey) + ":" + StringFormater.UrlEncode(credentials.ConsumerSecret);
            var credBytes         = Encoding.UTF8.GetBytes(concatenatedCredentials);
            var base64Credentials = Convert.ToBase64String(credBytes);

            return("Basic " + base64Credentials);
        }
        public IEnumerable <IOAuthQueryParameter> GenerateConsumerParameters(IConsumerCredentials consumerCredentials)
        {
            var consumerHeaders = new List <IOAuthQueryParameter>();

            // Add Header for every connection to a Twitter Application
            if (consumerCredentials != null && !String.IsNullOrEmpty(consumerCredentials.ConsumerKey) && !String.IsNullOrEmpty(consumerCredentials.ConsumerSecret))
            {
                consumerHeaders.Add(new OAuthQueryParameter("oauth_consumer_key", StringFormater.UrlEncode(consumerCredentials.ConsumerKey), true, true, false));
                consumerHeaders.Add(new OAuthQueryParameter("oauth_consumer_secret", StringFormater.UrlEncode(consumerCredentials.ConsumerSecret), false, false, true));
            }

            return(consumerHeaders);
        }
        public Task <ITwitterResult> UpdateProfileBanner(IUpdateProfileBannerParameters parameters, ITwitterRequest request)
        {
            var query             = _accountSettingsQueryGenerator.GetUpdateProfileBannerQuery(parameters);
            var banner            = StringFormater.UrlEncode(Convert.ToBase64String(parameters.Binary));
            var bannerHttpContent = new StringContent($"banner={banner}", Encoding.UTF8, "application/x-www-form-urlencoded");

            request.Query.Url         = query;
            request.Query.HttpMethod  = HttpMethod.POST;
            request.Query.HttpContent = new ProgressableStreamContent(bannerHttpContent, parameters.UploadProgressChanged);
            request.Query.IsHttpContentPartOfQueryParams = true;
            request.Query.Timeout = parameters.Timeout ?? TimeSpan.FromMilliseconds(System.Threading.Timeout.Infinite);

            return(_twitterAccessor.ExecuteRequest(request));
        }
예제 #7
0
        private string GenerateSignature(
            Uri uri,
            HttpMethod httpMethod,
            IEnumerable <IOAuthQueryParameter> queryParameters,
            Dictionary <string, string> urlParameters)
        {
            var oAuthQueryParameters   = queryParameters.ToArray();
            var signatureParameters    = GetSignatureParameters(oAuthQueryParameters, urlParameters);
            var formattedUrlParameters = CreateFormattedUrlParameters(signatureParameters);
            var oAuthRequest           = CreateOAuthRequest(uri, httpMethod, formattedUrlParameters);
            var oAuthSecretKey         = CreateOAuthSecretKey(oAuthQueryParameters);

            var hmacsha1Generator = new HMACSHA1Generator();

            return(StringFormater.UrlEncode(Convert.ToBase64String(hmacsha1Generator.ComputeHash(oAuthRequest, oAuthSecretKey, Encoding.UTF8))));
        }
예제 #8
0
        public static string GenerateFilterLocationRequest(List <ILocation> locations)
        {
            if (locations.IsNullOrEmpty())
            {
                return(String.Empty);
            }

            StringBuilder queryBuilder = new StringBuilder();

            // queryBuilder.Append("locations=");
            for (int i = 0; i < locations.Count - 1; ++i)
            {
                queryBuilder.Append(GenerateLocationParameters(locations[i], false));
            }

            queryBuilder.Append(GenerateLocationParameters(locations[locations.Count - 1], true));

            return(String.Format("locations={0}", StringFormater.UrlEncode(queryBuilder.ToString())));
        }
예제 #9
0
        /// <summary>
        /// Method Allowing to initialize a SortedDictionnary to enable oAuth query to be generated with
        /// these parameters
        /// </summary>
        /// <param name="method_uri">This is the Uri that will be required for creating the oAuth WebRequest</param>
        /// <returns>Call the method defined in the _generateDelegate and return a string result
        /// This result will be the header of the WebRequest.</returns>
        private string generateParameters(Uri method_uri)
        {
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
            string   oauth_timestamp = Convert.ToInt64(ts.TotalSeconds).ToString();

            string oauth_nonce = new Random().Next(123400, 9999999).ToString();

            SortedDictionary <String, String> parameters = new SortedDictionary <string, string>();

            parameters.Add("oauth_version", "1.0");
            parameters.Add("oauth_nonce", oauth_nonce);
            parameters.Add("oauth_timestamp", oauth_timestamp);
            parameters.Add("oauth_signature_method", "HMAC-SHA1");
            parameters.Add("oauth_consumer_key", StringFormater.UrlEncode(this.ConsumerKey));
            parameters.Add("oauth_token", StringFormater.UrlEncode(this.AccessToken));
            parameters.Add("oauth_consumer_secret", StringFormater.UrlEncode(this.ConsumerSecret));
            parameters.Add("oauth_token_secret", StringFormater.UrlEncode(this.AccessTokenSecret));

            return(_generateDelegate != null?_generateDelegate(parameters, method_uri) : null);
        }
        public IEnumerable <IOAuthQueryParameter> GenerateParameters(IOAuthCredentials credentials, IEnumerable <IOAuthQueryParameter> additionalParameters = null)
        {
            var headers = GenerateConsumerParameters(credentials).ToList();

            // Add Header for authenticated connection to a Twitter Application
            if (credentials != null && !String.IsNullOrEmpty(credentials.AccessToken) && !String.IsNullOrEmpty(credentials.AccessTokenSecret))
            {
                headers.Add(new OAuthQueryParameter("oauth_token", StringFormater.UrlEncode(credentials.AccessToken), true, true, false));
                headers.Add(new OAuthQueryParameter("oauth_token_secret", StringFormater.UrlEncode(credentials.AccessTokenSecret), false, false, true));
            }
            else
            {
                headers.Add(new OAuthQueryParameter("oauth_token", "", false, false, true));
            }

            if (additionalParameters != null)
            {
                headers.AddRange(additionalParameters);
            }

            return(headers);
        }
예제 #11
0
        /// <summary>
        /// Encryption of the data to be sent
        /// </summary>
        /// <param name="parameters">Parameters of the query</param>
        /// <param name="method_uri">Uri to be called</param>
        /// <returns>Header of the WebRequest</returns>
        private string generateSignature(SortedDictionary <String, String> parameters, Uri method_uri)
        {
            String[] secretParameters = new[] { "oauth_consumer_key", "oauth_nonce",
                                                "oauth_signature_method", "oauth_timestamp", "oauth_token", "oauth_version" };

            List <KeyValuePair <String, String> > orderedParams = new List <KeyValuePair <string, string> >();

            foreach (var query_param in _queryParameters)
            {
                orderedParams.Add(query_param);
            }

            foreach (var param in (from p in parameters orderby p.Key where (secretParameters.Contains(p.Key)) == true select p))
            {
                orderedParams.Add(param);
            }

            StringBuilder queryParameters = new StringBuilder();

            foreach (var param in (from p in orderedParams orderby p.Key select p))
            {
                if (queryParameters.Length > 0)
                {
                    queryParameters.Append("&");
                }
                queryParameters.Append(string.Format("{0}={1}", param.Key, param.Value));
            }

            string url          = method_uri.Query == "" ? method_uri.AbsoluteUri : method_uri.AbsoluteUri.Replace(method_uri.Query, "");
            string oAuthRequest = string.Format("{0}&{1}&{2}", _method.ToString(),
                                                StringFormater.UrlEncode(url), StringFormater.UrlEncode(queryParameters.ToString()));

            string oAuthSecretkey = string.Format("{0}&{1}",
                                                  StringFormater.UrlEncode(this.ConsumerSecret), StringFormater.UrlEncode(this.AccessTokenSecret));
            HMACSHA1 hasher = new HMACSHA1(new ASCIIEncoding().GetBytes(oAuthSecretkey));

            return(StringFormater.UrlEncode(Convert.ToBase64String(hasher.ComputeHash(new ASCIIEncoding().GetBytes(oAuthRequest)))));
        }
예제 #12
0
        public virtual IEnumerable <IOAuthQueryParameter> GenerateParameters()
        {
            List <IOAuthQueryParameter> headers = new List <IOAuthQueryParameter>();

            // Add Header for every connection to a Twitter Application
            if (!String.IsNullOrEmpty(Credentials.ConsumerKey) && !String.IsNullOrEmpty(Credentials.ConsumerSecret))
            {
                headers.Add(new OAuthQueryParameter("oauth_consumer_key", StringFormater.UrlEncode(Credentials.ConsumerKey), true, true, false));
                headers.Add(new OAuthQueryParameter("oauth_consumer_secret", StringFormater.UrlEncode(Credentials.ConsumerSecret), false, false, true));
            }

            // Add Header for authenticated connection to a Twitter Application
            if (!String.IsNullOrEmpty(Credentials.AccessToken) && !String.IsNullOrEmpty(Credentials.AccessTokenSecret))
            {
                headers.Add(new OAuthQueryParameter("oauth_token", StringFormater.UrlEncode(Credentials.AccessToken), true, true, false));
                headers.Add(new OAuthQueryParameter("oauth_token_secret", StringFormater.UrlEncode(Credentials.AccessTokenSecret), false, false, true));
            }
            else
            {
                headers.Add(new OAuthQueryParameter("oauth_token", "", false, false, true));
            }

            return(headers);
        }
예제 #13
0
        private string GenerateSignature(
            Uri uri,
            HttpMethod httpMethod,
            IEnumerable <IOAuthQueryParameter> queryParameters,
            Dictionary <string, string> urlParameters)
        {
            List <KeyValuePair <String, String> > signatureParameters = urlParameters.OrderBy(x => x.Key).ToList();

            #region Store the paramaters that will be used

            // Add all the parameters that are required to generate a signature
            var oAuthQueryParameters = queryParameters as IList <IOAuthQueryParameter> ?? queryParameters.ToList();
            foreach (var header in (from h in oAuthQueryParameters
                                    where h.RequiredForSignature
                                    orderby h.Key
                                    select h))
            {
                signatureParameters.Add(new KeyValuePair <string, string>(header.Key, header.Value));
            }

            #endregion

            #region Generate OAuthRequest Parameters

            StringBuilder urlParametersFormatted = new StringBuilder();
            foreach (KeyValuePair <string, string> param in (from p in signatureParameters orderby p.Key select p))
            {
                if (urlParametersFormatted.Length > 0)
                {
                    urlParametersFormatted.Append("&");
                }

                urlParametersFormatted.Append(string.Format("{0}={1}", param.Key, param.Value));
            }

            #endregion

            #region Generate OAuthRequest

            string url = uri.Query == "" ? uri.AbsoluteUri : uri.AbsoluteUri.Replace(uri.Query, "");

            string oAuthRequest = string.Format("{0}&{1}&{2}",
                                                httpMethod,
                                                StringFormater.UrlEncode(url),
                                                StringFormater.UrlEncode(urlParametersFormatted.ToString()));

            #endregion

            #region Generate OAuthSecretKey
            // Generate OAuthSecret that is required to generate a signature
            IEnumerable <IOAuthQueryParameter> oAuthSecretKeyHeaders = from h in oAuthQueryParameters
                                                                       where h.IsPartOfOAuthSecretKey
                                                                       orderby h.Key
                                                                       select h;
            string oAuthSecretkey = "";

            for (int i = 0; i < oAuthSecretKeyHeaders.Count(); ++i)
            {
                oAuthSecretkey += String.Format("{0}{1}",
                                                StringFormater.UrlEncode(oAuthSecretKeyHeaders.ElementAt(i).Value),
                                                (i == oAuthSecretKeyHeaders.Count() - 1) ? "" : "&");
            }

            #endregion

            // Create and return signature

            HMACSHA1Generator hmacsha1Generator = new HMACSHA1Generator();
            return(StringFormater.UrlEncode(Convert.ToBase64String(hmacsha1Generator.ComputeHash(oAuthRequest, oAuthSecretkey, Encoding.UTF8))));
        }
예제 #14
0
 public IOAuthQueryParameter GenerateParameter(string key, string value, bool requiredForSignature, bool requiredForHeader, bool isPartOfOAuthSecretKey)
 {
     return(new OAuthQueryParameter(key, StringFormater.UrlEncode(value), requiredForSignature, requiredForHeader, isPartOfOAuthSecretKey));
 }