Exemple #1
0
        public virtual HttpWebRequest GetQueryWebRequest(string url, HttpMethod httpMethod, IEnumerable <IOAuthQueryParameter> headers = null)
        {
            if (headers == null)
            {
                headers = _oAuthWebRequestGenerator.GenerateParameters(_credentialsAccessor.CurrentThreadCredentials);
            }

            return(_oAuthWebRequestGenerator.GenerateWebRequest(url, httpMethod, headers));
        }
        protected virtual Task <HttpResponseMessage> SendAsync(ITwitterQuery twitterQuery, HttpRequestMessage request, CancellationToken cancellationToken)
        {
            if (_action != null)
            {
                _action(twitterQuery, request);
            }

            string authorizationHeader;

            if (_func != null)
            {
                authorizationHeader = _func(twitterQuery, request);
            }
            else
            {
                var credentials = twitterQuery.TwitterCredentials;

                if (!string.IsNullOrEmpty(credentials.AccessToken) &&
                    !string.IsNullOrEmpty(credentials.AccessTokenSecret))
                {
                    var uri = new Uri(twitterQuery.QueryURL);
                    var credentialsParameters = _webRequestGenerator.GenerateParameters(twitterQuery.TwitterCredentials);
                    authorizationHeader = _webRequestGenerator.GenerateAuthorizationHeader(uri, twitterQuery.HttpMethod, credentialsParameters);
                }
                else
                {
                    authorizationHeader = string.Format("Bearer {0}", credentials.ApplicationOnlyBearerToken);
                }
            }

            return(SendAsync(request, cancellationToken, authorizationHeader));
        }
Exemple #3
0
        public IEnumerable <IOAuthQueryParameter> GetCredentialsQueryParameters(ITwitterQuery twitterQuery)
        {
            var queryParameters = twitterQuery.QueryParameters;

            if (twitterQuery.TwitterCredentials == null)
            {
                if (_credentialsAccessor.CurrentThreadCredentials == null)
                {
                    throw new TwitterNullCredentialsException();
                }

                twitterQuery.TwitterCredentials = _credentialsAccessor.CurrentThreadCredentials;
            }

            if (queryParameters == null)
            {
                queryParameters = _webRequestGenerator.GenerateParameters(twitterQuery.TwitterCredentials);
            }

            return(queryParameters);
        }
        public VmAuthorizationHeader Sign([FromBody] VmRequestToSign req)
        {
            // Check that the Consumer Key in the request matches the one that we have
            if (req.ConsumerKey != CONSUMER_KEY)
            {
                throw new ArgumentException(
                          "Request must be for a Twitter Application that we have the credentials for", nameof(req));
            }

            Uri uri = new Uri(req.Url);
            TwitterCredentials twitterCredentials = new TwitterCredentials(req.ConsumerKey, CONSUMER_SECRET,
                                                                           req.ClientToken, req.ClientSecret);

            IOAuthWebRequestGenerator          oAuthWebReqGenerator = TweetinviContainer.Resolve <IOAuthWebRequestGenerator>();
            IEnumerable <IOAuthQueryParameter> credentialsParams    =
                oAuthWebReqGenerator.GenerateParameters(twitterCredentials);
            string authHeader = oAuthWebReqGenerator.GenerateAuthorizationHeader(uri, req.Method, credentialsParams);

            return(new VmAuthorizationHeader()
            {
                AuthorizationHeader = authHeader
            });
        }