예제 #1
0
        public void GenerateSignature()
        {
            var signatureBase = "POST&https%3A%2F%2Fapi.twitter.com%2F1.1%2Fstatuses%2Fupdate.json&include_entities%3Dtrue%26oauth_consumer_key%3Dxvz1evFS4wEEPTGEFPHBog%26oauth_nonce%3DkYjzVBB8Y0ZFabxSWbWovY3uYSQ2pTgmZeNu2VS4cg%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1318622958%26oauth_token%3D370773112-GmHxMAgYyLbNEtIKZeRNFsMKPR9EyMZeS9weJAEb%26oauth_version%3D1.0%26status%3DHello%2520Ladies%2520%252B%2520Gentlemen%252C%2520a%2520signed%2520OAuth%2520request%2521";

            var consumerSecret = "kAcSOqF21Fu85e7zjz7ZN2U4ZRhfV3WpwPAoE3Z7kBw";
            var tokenSecret    = "LswwdoUaIvS8ltyTt5jkRh4J50vUPVVHtR2YPi5kE";

            var expected = "hCtSmYh+iHYCEqBWrE7C7hYmtUk=";
            var actual   = OAuthHelper.GenerateSignature(consumerSecret, tokenSecret, signatureBase);

            Assert.Equal(expected, actual);
        }
        /// <summary>
        /// Gets a signed Uri for the specified endpoint with the specified parameters appended to the query string.
        /// This Uri can be used with REST APIs that use OAuth.
        /// </summary>
        /// <param name="url">The url that will have the parameters appended to its query string</param>
        /// <param name="key">OAuth key required by the external system</param>
        /// <param name="secret">OAuth secret required by the external system</param>
        /// <param name="parameters">The keys and values that are appended to the query string</param>
        /// <returns>A signed Uri for the endpoint with the parameters appended to the query string</returns>
        protected virtual Uri GetSignedUri(string url, string key, string secret, IDictionary<string, string> parameters)
        {
            var uri = GetUri(url, parameters);
            string nurl, nreq;
            var oAuth = new OAuthHelper();

            var nounce = oAuth.GenerateNonce();
            var timestamp = oAuth.GenerateTimeStamp();

            var signatureUrl = oAuth.GenerateSignature(
                url: uri,
                consumerKey: key,
                consumerSecret: secret,
                token: string.Empty,
                tokenSecret: string.Empty,
                httpMethod: "GET",
                timeStamp: timestamp,
                nonce: nounce,
                signatureType: OAuthHelper.SignatureTypes.HMACSHA1,
                normalizedUrl: out nurl,
                normalizedRequestParameters: out nreq);

            signatureUrl = HttpUtility.UrlEncode(signatureUrl);

            var parameters2 = new Dictionary<string, string>();
            parameters2.Add("oauth_consumer_key", key);
            parameters2.Add("oauth_nonce", nounce);
            parameters2.Add("oauth_timestamp", timestamp);
            parameters2.Add("oauth_signature_method", "HMAC-SHA1");
            parameters2.Add("oauth_version", "1.0");
            parameters2.Add("oauth_signature", signatureUrl);
            var uri2 = GetUri(uri.ToString(), parameters2, true);
            return uri2;
        }