예제 #1
0
		public OAuthMessageHandler(HttpMessageHandler innerHandler, IHmacSha1HashProvider hashProvider, string consumerKey, string consumerSecret, Token oAuthToken)
			: base(innerHandler)
		{
            if (hashProvider == null)
                throw new ArgumentNullException("hashProvider");

			if (consumerKey == null)
				throw new ArgumentNullException("consumerKey");

			if (consumerKey.Length == 0)
				throw new ArgumentException("Consumer key cannot be empty.", "consumerKey");

			if (consumerSecret == null)
				throw new ArgumentNullException("consumerSecret");

			if (consumerSecret.Length == 0)
				throw new ArgumentException("Consumer secret cannot be empty.", "consumerSecret");

			if(oAuthToken != null && !oAuthToken.IsValid)
				throw new ArgumentException("OAuth token is not valid.", "oAuthToken");

            if (consumerKey == null)
                throw new ArgumentNullException("consumerKey");

            this.hashProvider = hashProvider;
			this.consumerKey = consumerKey;
			this.consumerSecret = consumerSecret;
			this.oAuthToken = oAuthToken;
		}
예제 #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="OAuth"/> client class.
        /// </summary>
        /// <param name="hashProvider">
        /// A <see cref="IHmacSha1HashProvider"/> implementation used to generate a
        /// HMAC-SHA1 hash for OAuth purposes.
        /// </param>
        /// <param name="consumerKey">
        /// The consumer key.
        /// </param>
        /// <param name="consumerSecret">
        /// The consumer secret.
        /// </param>
        /// <remarks>
        /// You can get a consumer key and a consumer secret by registering an application with Tumblr:<br/>
        /// <br/>
        /// http://www.tumblr.com/oauth/apps
        /// </remarks>
        /// <exception cref="ArgumentNullException">
        /// <list type="bullet">
        /// <item>
        ///		<description>
        ///			<paramref name="hashProvider"/> is <b>null</b>.
        ///		</description>
        ///	</item>
        /// <item>
        ///		<description>
        ///			<paramref name="consumerKey"/> is <b>null</b>.
        ///		</description>
        ///	</item>
        ///	<item>
        ///		<description>
        ///			<paramref name="consumerSecret"/> is <b>null</b>.
        ///		</description>
        ///	</item>
        /// </list>
        /// </exception>
        /// <exception cref="ArgumentException">
        /// <list type="bullet">
        /// <item>
        ///		<description>
        ///			<paramref name="consumerKey"/> is empty.
        ///		</description>
        ///	</item>
        ///	<item>
        ///		<description>
        ///			<paramref name="consumerSecret"/> is empty.
        ///		</description>
        ///	</item>
        /// </list>
        /// </exception>
        public OAuthClient(IHmacSha1HashProvider hashProvider, string consumerKey, string consumerSecret)
        {
            if (hashProvider == null)
            {
                throw new ArgumentNullException("hashProvider");
            }

            if (consumerKey == null)
            {
                throw new ArgumentNullException("consumerKey");
            }

            if (consumerKey.Length == 0)
            {
                throw new ArgumentException("Consumer key cannot be empty.", "consumerKey");
            }

            if (consumerSecret == null)
            {
                throw new ArgumentNullException("consumerSecret");
            }

            if (consumerSecret.Length == 0)
            {
                throw new ArgumentException("Consumer secret cannot be empty.", "consumerSecret");
            }

            this.hashProvider   = hashProvider;
            this.consumerKey    = consumerKey;
            this.consumerSecret = consumerSecret;
        }
예제 #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="TumblrClientBase"/> class.
        /// </summary>
        /// <param name="hashProvider">
        /// A <see cref="IHmacSha1HashProvider"/> implementation used to generate a
        /// HMAC-SHA1 hash for OAuth purposes.
        /// </param>
        /// <param name="consumerKey">
        /// The consumer key.
        /// </param>
        /// <param name="consumerSecret">
        /// The consumer secret.
        /// </param>
        /// <param name="oAuthToken">
        /// An optional access token for the API. If no access token is provided, only the methods
        /// that do not require OAuth can be invoked successfully.
        /// </param>
        /// <remarks>
        ///  You can get a consumer key and a consumer secret by registering an application with Tumblr:<br/>
        /// <br/>
        /// http://www.tumblr.com/oauth/apps
        /// </remarks>
        public TumblrClientBase(IHmacSha1HashProvider hashProvider, string consumerKey, string consumerSecret, Token oAuthToken = null)
        {
            if (consumerKey == null)
            {
                throw new ArgumentNullException("consumerKey");
            }

            if (consumerKey.Length == 0)
            {
                throw new ArgumentException("Consumer key cannot be empty.", "consumerKey");
            }

            if (consumerSecret == null)
            {
                throw new ArgumentNullException("consumerSecret");
            }

            if (consumerSecret.Length == 0)
            {
                throw new ArgumentException("Consumer secret cannot be empty.", "consumerSecret");
            }

            this.oAuthToken = oAuthToken;
            this.client     = new HttpClient(new OAuthMessageHandler(hashProvider, consumerKey, consumerSecret, oAuthToken));
        }
예제 #4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="TumblrClientBase"/> class.
        /// </summary>
        /// <param name="httpClientFactory"> <see cref="IHttpClientFactory">HttpClientFactory</see> to create internal HttpClient</param>
        /// <param name="hashProvider">
        /// A <see cref="IHmacSha1HashProvider"/> implementation used to generate a
        /// HMAC-SHA1 hash for OAuth purposes.
        /// </param>
        /// <param name="consumerKey">
        /// The consumer key.
        /// </param>
        /// <param name="consumerSecret">
        /// The consumer secret.
        /// </param>
        /// <param name="oAuthToken">
        /// An optional access token for the API. If no access token is provided, only the methods
        /// that do not require OAuth can be invoked successfully.
        /// </param>
        /// <remarks>
        ///  You can get a consumer key and a consumer secret by registering an application with Tumblr:<br/>
        /// http://www.tumblr.com/oauth/apps
        /// <br/><br/>platform: .NetStandard 2.0+, .Net Core 2.2+
        /// </remarks>
        public TumblrClientBase(IHttpClientFactory httpClientFactory, IHmacSha1HashProvider hashProvider, string consumerKey, string consumerSecret, Token oAuthToken = null)
        {
            if (httpClientFactory == null)
            {
                throw new ArgumentNullException(nameof(httpClientFactory));
            }

            if (consumerKey == null)
            {
                throw new ArgumentNullException(nameof(consumerKey));
            }

            if (consumerKey.Length == 0)
            {
                throw new ArgumentException("Consumer key cannot be empty.", nameof(consumerKey));
            }

            if (consumerSecret == null)
            {
                throw new ArgumentNullException(nameof(consumerSecret));
            }

            if (consumerSecret.Length == 0)
            {
                throw new ArgumentException("Consumer secret cannot be empty.", nameof(consumerSecret));
            }

            if (oAuthToken != null)
            {
                if (oAuthToken.IsValid == false)
                {
                    throw new ArgumentException("oAuthToken is not valid", nameof(oAuthToken));
                }
            }

            this.oAuthToken = oAuthToken;

            _consumerKey    = consumerKey;
            _consumerSecret = consumerSecret;

            _hashProvider = hashProvider;

            this.httpClient = httpClientFactory.CreateClient();
        }
예제 #5
0
        /// <summary>
        /// Create OAuthMessageHandle
        /// </summary>
        /// <param name="innerHandler"></param>
        /// <param name="hashProvider"></param>
        /// <param name="consumerKey"></param>
        /// <param name="consumerSecret"></param>
        /// <param name="oAuthToken"></param>
        public OAuthMessageHandler(HttpMessageHandler innerHandler, IHmacSha1HashProvider hashProvider, string consumerKey, string consumerSecret, Token oAuthToken)
            : base(innerHandler)
        {
            if (hashProvider == null)
            {
                throw new ArgumentNullException(nameof(hashProvider));
            }

            if (consumerKey == null)
            {
                throw new ArgumentNullException(nameof(consumerKey));
            }

            if (consumerKey.Length == 0)
            {
                throw new ArgumentException("Consumer key cannot be empty.", nameof(consumerKey));
            }

            if (consumerSecret == null)
            {
                throw new ArgumentNullException(nameof(consumerSecret));
            }

            if (consumerSecret.Length == 0)
            {
                throw new ArgumentException("Consumer secret cannot be empty.", nameof(consumerSecret));
            }

            if (oAuthToken != null && !oAuthToken.IsValid)
            {
                throw new ArgumentException("OAuth token is not valid.", nameof(oAuthToken));
            }

            if (consumerKey == null)
            {
                throw new ArgumentNullException(nameof(consumerKey));
            }

            this.hashProvider   = hashProvider;
            this.consumerKey    = consumerKey;
            this.consumerSecret = consumerSecret;
            this.oAuthToken     = oAuthToken;
        }
예제 #6
0
		public OAuthMessageHandler(IHmacSha1HashProvider hashProvider, string consumerKey, string consumerSecret, Token oAuthToken)
            : this(new HttpClientHandler(), hashProvider, consumerKey, consumerSecret, oAuthToken)
		{ }
예제 #7
0
 /// <summary>
 /// Create OAuthMessageHandler
 /// </summary>
 /// <param name="hashProvider"></param>
 /// <param name="consumerKey"></param>
 /// <param name="consumerSecret"></param>
 /// <param name="oAuthToken"></param>
 public OAuthMessageHandler(IHmacSha1HashProvider hashProvider, string consumerKey, string consumerSecret, Token oAuthToken)
     : this(new HttpClientHandler(), hashProvider, consumerKey, consumerSecret, oAuthToken)
 {
 }
예제 #8
0
        /// <summary>
        /// set the authoriaztionheader for TumblrClient
        /// </summary>
        /// <param name="request"></param>
        /// <param name="hashProvider"></param>
        /// <param name="consumerKey"></param>
        /// <param name="consumerSecret"></param>
        /// <param name="oAuthToken"></param>
        /// <returns></returns>
        public static async Task PreparationForTumblrClient(this HttpRequestMessage request, IHmacSha1HashProvider hashProvider, string consumerKey, string consumerSecret, Token oAuthToken)
        {
            MethodParameterSet requestParameters = null;

            if (request.Content is FormUrlEncodedContent)
            {
                var formUrlEncoded = request.Content as FormUrlEncodedContent;

                string content = await formUrlEncoded.ReadAsStringAsync().ConfigureAwait(false);

                requestParameters = new MethodParameterSet(content);
            }
            else if (request.Content is MultipartFormDataContent multiPartContent)
            {
                requestParameters = new MethodParameterSet();

                foreach (var c in multiPartContent)
                {
                    if (c is StringContent stringContent)
                    {
                        requestParameters.Add(c.Headers.ContentDisposition.Name, await c.ReadAsStringAsync().ConfigureAwait(false));
                    }
                }
            }

            //if we have an api_key parameter we can skip the oauth
            if (requestParameters.FirstOrDefault(c => c.Name == "api_key") == null)
            {
                var authorizationHeaderParameters = new MethodParameterSet(requestParameters);

                if (oAuthToken != null)
                {
                    authorizationHeaderParameters.Add("oauth_token", oAuthToken.Key);
                }

                authorizationHeaderParameters.Add("oauth_consumer_key", consumerKey);
                authorizationHeaderParameters.Add("oauth_nonce", Guid.NewGuid().ToString());
                authorizationHeaderParameters.Add("oauth_timestamp", DateTimeHelper.ToTimestamp(DateTime.UtcNow).ToString());
                authorizationHeaderParameters.Add("oauth_signature_method", "HMAC-SHA1");
                authorizationHeaderParameters.Add("oauth_version", "1.0");

                string urlParameters = authorizationHeaderParameters.ToFormUrlEncoded();

                var requestUriNoQueryString = request.RequestUri.OriginalString;

                if (!String.IsNullOrEmpty(request.RequestUri.Query))
                {
                    requestUriNoQueryString = request.RequestUri.OriginalString.Replace(request.RequestUri.Query, String.Empty);
                }

                string signatureBaseString = String.Format("{0}&{1}&{2}", request.Method.ToString(), UrlEncoder.Encode(requestUriNoQueryString), UrlEncoder.Encode(urlParameters));
                string signatureHash       = hashProvider.ComputeHash(consumerSecret, oAuthToken?.Secret, signatureBaseString);

                authorizationHeaderParameters.Add("oauth_signature", signatureHash);

                foreach (IMethodParameter p in requestParameters)
                {
                    //remove non-oauth parameters from the authorization header
                    if (!p.Name.StartsWith("oauth"))
                    {
                        authorizationHeaderParameters.Remove(p);
                    }
                }

                request.Headers.Authorization = new AuthenticationHeaderValue("OAuth", authorizationHeaderParameters.ToAuthorizationHeader());
            }

            if (request.Method == HttpMethod.Get)
            {
                request.Content = null;                 //we don't have to send a body with get requests
            }
        }
예제 #9
0
		/// <summary>
		/// Initializes a new instance of the <see cref="TumblrClient"/> class.
		/// </summary>
        /// <param name="hashProvider">
        /// A <see cref="IHmacSha1HashProvider"/> implementation used to generate a
        /// HMAC-SHA1 hash for OAuth purposes.
        /// </param>
		/// <param name="consumerKey">
		/// The consumer key.
		/// </param>
		/// <param name="consumerSecret">
		/// The consumer secret.
		/// </param>
		/// <param name="oAuthToken">
		/// An optional access token for the API. If no access token is provided, only the methods
		/// that do not require OAuth can be invoked successfully.
		/// </param>
		/// <remarks>
		///  You can get a consumer key and a consumer secret by registering an application with Tumblr:<br/>
		/// <br/>
		/// http://www.tumblr.com/oauth/apps
		/// </remarks>
        public TumblrClient(IHmacSha1HashProvider hashProvider, string consumerKey, string consumerSecret, Token oAuthToken = null)
			: base(hashProvider, consumerKey, consumerSecret, oAuthToken)
		{
			this.apiKey = consumerKey; 
		}
예제 #10
0
		/// <summary>
		/// Initializes a new instance of the <see cref="OAuth"/> client class.
		/// </summary>
        /// <param name="hashProvider">
        /// A <see cref="IHmacSha1HashProvider"/> implementation used to generate a
        /// HMAC-SHA1 hash for OAuth purposes.
        /// </param>
		/// <param name="consumerKey">
		/// The consumer key.
		/// </param>
		/// <param name="consumerSecret">
		/// The consumer secret.
		/// </param>
		/// <remarks>
		/// You can get a consumer key and a consumer secret by registering an application with Tumblr:<br/>
		/// <br/>
		/// http://www.tumblr.com/oauth/apps
		/// </remarks>
		/// <exception cref="ArgumentNullException">
		/// <list type="bullet">
        /// <item>
        ///		<description>
        ///			<paramref name="hashProvider"/> is <b>null</b>.
        ///		</description>
        ///	</item>
		/// <item>
		///		<description>
		///			<paramref name="consumerKey"/> is <b>null</b>.
		///		</description>
		///	</item>
		///	<item>
		///		<description>
		///			<paramref name="consumerSecret"/> is <b>null</b>.
		///		</description>
		///	</item>
		/// </list>
		/// </exception>
		/// <exception cref="ArgumentException">
		/// <list type="bullet">
		/// <item>
		///		<description>
		///			<paramref name="consumerKey"/> is empty.
		///		</description>
		///	</item>
		///	<item>
		///		<description>
		///			<paramref name="consumerSecret"/> is empty.
		///		</description>
		///	</item>
		/// </list>
		/// </exception>
		public OAuthClient(IHmacSha1HashProvider hashProvider, string consumerKey, string consumerSecret)
		{
            if (hashProvider == null)
                throw new ArgumentNullException("hashProvider");

			if (consumerKey == null)
				throw new ArgumentNullException("consumerKey");

			if (consumerKey.Length == 0)
				throw new ArgumentException("Consumer key cannot be empty.", "consumerKey");

			if (consumerSecret == null)
				throw new ArgumentNullException("consumerSecret");

			if (consumerSecret.Length == 0)
				throw new ArgumentException("Consumer secret cannot be empty.", "consumerSecret");

            this.hashProvider = hashProvider;
			this.consumerKey = consumerKey;
			this.consumerSecret = consumerSecret;
		}