Ejemplo n.º 1
0
        public string GenerateAuthorizationHeader(Uri uri, HttpMethod httpMethod, IEnumerable <IOAuthQueryParameter> parameters)
        {
            var queryParameters = GenerateAdditionalHeaderParameters(parameters);
            var urlParameters   = _webHelper.GetUriParameters(uri);

            return(GenerateHeader(uri, httpMethod, queryParameters, urlParameters));
        }
        // ReSharper disable once InconsistentNaming
        //  Autofac requires variable names to match, not just the method signature :s
        public TwitterQueryRemoteAuth(string queryURL, HttpMethod httpMethod) : base(queryURL, httpMethod)
        {
            // Get the credentials to use to sign this request
            ITwitterCredentials twitterCredentials = TwitterCredentials;

            if (twitterCredentials == null)
            {
                ICredentialsAccessor credentialsAccessor = TweetinviContainer.Resolve <ICredentialsAccessor>();
                twitterCredentials = credentialsAccessor.CurrentThreadCredentials;
            }

            // Send a request to the TweetinviRequestSigner server to sign this request
            VmRequestToSign signReq = new VmRequestToSign()
            {
                Url          = queryURL,
                Method       = httpMethod,
                ConsumerKey  = twitterCredentials.ConsumerKey,
                ClientToken  = twitterCredentials.AccessToken,
                ClientSecret = twitterCredentials.AccessTokenSecret
            };

            // TODO: Handle Exceptions through Tweetinvi?
            VmAuthorizationHeader vmAuthHeader = Requester.MakePostJson <VmAuthorizationHeader>("Request/Sign", signReq);

            AuthorizationHeader = vmAuthHeader.AuthorizationHeader;
        }
Ejemplo n.º 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}");
        }
Ejemplo n.º 4
0
 // Sign
 public static ITwitterRequestParameters GenerateTwitterRequestParameters(
     string query,
     HttpMethod method,
     ITwitterCredentials credentials = null,
     HttpContent content             = null)
 {
     return(Accessor.GenerateTwitterRequestParameters(query, method, credentials, content));
 }
Ejemplo n.º 5
0
        public T ExecuteQuery <T>(
            string query,
            HttpMethod method,
            ITwitterCredentials credentials,
            HttpContent httpContent) where T : class
        {
            var webRequestResult = ExecuteQuery(query, method, credentials, httpContent);
            var jsonResponse     = webRequestResult.Text;

            return(_jsonObjectConverter.DeserializeObject <T>(jsonResponse));
        }
Ejemplo n.º 6
0
        public TwitterQuery(string queryURL, HttpMethod httpMethod)
        {
            QueryURL   = queryURL;
            HttpMethod = httpMethod;

            AcceptHeaders = new List <string>
            {
                "image/jpeg",
                "application/json"
            };
        }
Ejemplo n.º 7
0
        // Concrete Execute
        public IWebRequestResult ExecuteQuery(
            string query,
            HttpMethod method,
            ITwitterCredentials credentials,
            HttpContent httpContent = null)
        {
            if (query == null)
            {
                // When a query is null and has been generated by Tweetinvi it implies that one of the query parameter was invalid
                throw new ArgumentException("At least one of the arguments provided to the query was invalid.");
            }

            return(_twitterRequestHandler.ExecuteQuery(query, method, httpContent: httpContent, credentials: credentials));
        }
Ejemplo n.º 8
0
        private string GenerateHeader(
            Uri uri,
            HttpMethod httpMethod,
            ICollection <IOAuthQueryParameter> queryParameters,
            Dictionary <string, string> urlParameters)
        {
            var signature      = GenerateSignature(uri, httpMethod, queryParameters, urlParameters);
            var oAuthSignature = new OAuthQueryParameter("oauth_signature", signature, false, false, false);

            queryParameters.Add(oAuthSignature);

            var parametersFormattedForHeader = CreateParametersFormattedForHeader(queryParameters);
            var headerSignature = $"oauth_signature=\"{signature}\"";

            return($"OAuth {parametersFormattedForHeader},{headerSignature}");
        }
Ejemplo n.º 9
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))));
        }
Ejemplo n.º 10
0
        public string ExecuteQueryReturningContent(string query, HttpMethod method, HttpContent httpContent = null, bool forceThrow = false)
        {
            try
            {
                return(ExecuteQuery(query, method, (ITwitterCredentials)null, httpContent).Text);
            }
            catch (TwitterException ex)
            {
                if (forceThrow)
                {
                    throw;
                }

                HandleQueryException(ex);
                return(null);
            }
        }
Ejemplo n.º 11
0
        public async Task <string> GenerateAuthorizationHeaderAsync(
            Uri uri,
            HttpContent queryContent,
            HttpMethod httpMethod,
            IEnumerable <IOAuthQueryParameter> parameters)
        {
            var queryParameters = GenerateAdditionalHeaderParameters(parameters);
            var urlParameters   = _webHelper.GetUriParameters(uri);

            if (queryContent != null)
            {
                var query = await queryContent.ReadAsStringAsync().ConfigureAwait(false);

                var additionalParameters = _webHelper.GetQueryParameters(query);

                additionalParameters.ForEach(x => { urlParameters.Add(x.Key, x.Value); });
            }

            return(GenerateHeader(uri, httpMethod, queryParameters, urlParameters));
        }
Ejemplo n.º 12
0
        public string ExecuteQuery(
            string queryURL,
            HttpMethod httpMethod,
            TwitterClientHandler handler    = null,
            ITwitterCredentials credentials = null,
            HttpContent httpContent         = null)
        {
            queryURL = CleanupQueryURL(queryURL);
            var rateLimitTrackerOption = _tweetinviSettingsAccessor.RateLimitTrackerMode;

            var requestParameters = new HttpRequestParameters
            {
                Query       = queryURL,
                HttpMethod  = httpMethod,
                HttpContent = httpContent
            };

            ITwitterQuery twitterQuery;

            if (!TryPrepareRequest(requestParameters, rateLimitTrackerOption, credentials, out twitterQuery))
            {
                return(null);
            }

            try
            {
                var webRequestResult = _webRequestExecutor.ExecuteQuery(twitterQuery, handler);

                QueryCompleted(twitterQuery, webRequestResult, rateLimitTrackerOption);

                return(webRequestResult.Response);
            }
            catch (TwitterException ex)
            {
                HandleException(queryURL, rateLimitTrackerOption, ex.StatusCode, twitterQuery);

                throw;
            }
        }
Ejemplo n.º 13
0
        // Concrete Execute
        public string ExecuteQuery(string query, HttpMethod method, HttpContent httpContent, bool forceThrow = false)
        {
            if (query == null)
            {
                // When a query is null and has been generated by Tweetinvi it implies that one of the query parameter was invalid
                throw new ArgumentException("At least one of the arguments provided to the query was invalid.");
            }

            try
            {
                return(_twitterRequestHandler.ExecuteQuery(query, method, httpContent: httpContent));
            }
            catch (TwitterException ex)
            {
                if (forceThrow)
                {
                    throw;
                }

                HandleQueryException(ex);
                return(null);
            }
        }
Ejemplo n.º 14
0
 // Consumer Credentials
 public IWebRequestResult ExecuteQuery(string query, HttpMethod method,
                                       IConsumerOnlyCredentials credentials, HttpContent httpContent)
 {
     return(ExecuteQuery(query, method, new TwitterCredentials(credentials), httpContent));
 }
Ejemplo n.º 15
0
 public static IWebRequestResult ExecuteConsumerQuery(string query, HttpMethod method, IConsumerOnlyCredentials credentials)
 {
     return(Accessor.ExecuteQuery(query, method, credentials, null));
 }
Ejemplo n.º 16
0
 public TwitterQuery(string queryURL, HttpMethod httpMethod)
 {
     QueryURL = queryURL;
     HttpMethod = httpMethod;
 }
Ejemplo n.º 17
0
 public TwitterQuery(string queryURL, HttpMethod httpMethod)
 {
     QueryURL   = queryURL;
     HttpMethod = httpMethod;
 }
Ejemplo n.º 18
0
 public TwitterQuery(string queryURL, HttpMethod httpMethod) : this()
 {
     Url        = queryURL;
     HttpMethod = httpMethod;
 }
Ejemplo n.º 19
0
        // Sign
        public ITwitterRequestParameters GenerateTwitterRequestParameters(string url, HttpMethod method, ITwitterCredentials credentials, HttpContent httpContent)
        {
            var requestParameters = new HttpRequestParameters
            {
                Query       = url,
                HttpMethod  = method,
                HttpContent = httpContent
            };

            var twitterQuery = _twitterRequestHandler.GetTwitterQuery(requestParameters, RateLimitTrackerMode.None, credentials);

            return(new TwitterRequestParameters()
            {
                QueryURL = twitterQuery.QueryURL,
                HttpMethod = twitterQuery.HttpMethod,
                HttpContent = twitterQuery.HttpContent,
                AuthorizationHeader = twitterQuery.AuthorizationHeader,
                AcceptHeaders = twitterQuery.AcceptHeaders
            });
        }
Ejemplo n.º 20
0
 public string ExecuteQuery(string query, HttpMethod method)
 {
     return(ExecuteQuery(query, method, null));
 }
Ejemplo n.º 21
0
 public IWebRequestResult ExecuteQuery(string query, HttpMethod method)
 {
     return(ExecuteQuery(query, method, null));
 }
Ejemplo n.º 22
0
 /// <summary>
 /// Execute a query that returns WebRequestResult
 /// </summary>
 public static IWebRequestResult ExecuteQueryWithDetails(string query, HttpMethod method)
 {
     return(Accessor.ExecuteQuery(query, method));
 }
Ejemplo n.º 23
0
 public T ExecuteQuery <T>(string query, HttpMethod method, IConsumerOnlyCredentials credentials,
                           HttpContent httpContent) where T : class
 {
     return(ExecuteQuery <T>(query, method, new TwitterCredentials(credentials), httpContent));
 }
Ejemplo n.º 24
0
 // Base call
 /// <summary>
 /// Execute a query that returns json
 /// </summary>
 public static string ExecuteQuery(string query, HttpMethod method)
 {
     return(Accessor.ExecuteQuery(query, method)?.Text);
 }