Exemplo 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;
        }
Exemplo 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}");
        }
Exemplo 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));
 }
Exemplo 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));
        }
Exemplo n.º 6
0
        public TwitterQuery(string queryURL, HttpMethod httpMethod)
        {
            QueryURL   = queryURL;
            HttpMethod = httpMethod;

            AcceptHeaders = new List <string>
            {
                "image/jpeg",
                "application/json"
            };
        }
Exemplo 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));
        }
Exemplo 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}");
        }
Exemplo 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))));
        }
Exemplo 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);
            }
        }
Exemplo 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));
        }
Exemplo 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;
            }
        }
Exemplo 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);
            }
        }
Exemplo n.º 14
0
 // Consumer Credentials
 public IWebRequestResult ExecuteQuery(string query, HttpMethod method,
                                       IConsumerOnlyCredentials credentials, HttpContent httpContent)
 {
     return(ExecuteQuery(query, method, new TwitterCredentials(credentials), httpContent));
 }
Exemplo n.º 15
0
 public static IWebRequestResult ExecuteConsumerQuery(string query, HttpMethod method, IConsumerOnlyCredentials credentials)
 {
     return(Accessor.ExecuteQuery(query, method, credentials, null));
 }
Exemplo n.º 16
0
 public TwitterQuery(string queryURL, HttpMethod httpMethod)
 {
     QueryURL = queryURL;
     HttpMethod = httpMethod;
 }
Exemplo n.º 17
0
 public TwitterQuery(string queryURL, HttpMethod httpMethod)
 {
     QueryURL   = queryURL;
     HttpMethod = httpMethod;
 }
Exemplo n.º 18
0
 public TwitterQuery(string queryURL, HttpMethod httpMethod) : this()
 {
     Url        = queryURL;
     HttpMethod = httpMethod;
 }
Exemplo 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
            });
        }
Exemplo n.º 20
0
 public string ExecuteQuery(string query, HttpMethod method)
 {
     return(ExecuteQuery(query, method, null));
 }
Exemplo n.º 21
0
 public IWebRequestResult ExecuteQuery(string query, HttpMethod method)
 {
     return(ExecuteQuery(query, method, null));
 }
Exemplo n.º 22
0
 /// <summary>
 /// Execute a query that returns WebRequestResult
 /// </summary>
 public static IWebRequestResult ExecuteQueryWithDetails(string query, HttpMethod method)
 {
     return(Accessor.ExecuteQuery(query, method));
 }
Exemplo 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));
 }
Exemplo 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);
 }