コード例 #1
0
        /// <summary>
        /// This method generates an OAuth signature
        /// </summary>
        /// <param name="URL"></param>
        /// <param name="method"></param>
        /// <param name="body"></param>
        /// <returns></returns>
        public static String GenerateSignature(String URL, String method, String body, String clientId, AsymmetricAlgorithm privateKey)
        {
            OAuthParameters oAuthParameters = new OAuthParameters();

            oAuthParameters.setOAuthConsumerKey(clientId);
            oAuthParameters.setOAuthNonce(OAuthUtil.GetNonce());
            //oAuthParameters.setOAuthNonce("ssxECu3Ww1rURm2tM");
            oAuthParameters.setOAuthTimestamp(OAuthUtil.GetTimestamp());
            //oAuthParameters.setOAuthTimestamp("1466961120");
            oAuthParameters.setOAuthSignatureMethod("RSA-SHA256");
            oAuthParameters.setOAuthVersion("1.0");

            //Console.WriteLine("body: " + body);

            if (body != null)
            {
                String encodedHash = Util.Base64Encode(Util.Sha256Encode(body));
                //Console.WriteLine("bodyHash: " + encodedHash);
                oAuthParameters.setOAuthBodyHash(encodedHash);
            }

            String baseString = OAuthUtil.GetBaseString(URL, method, oAuthParameters.getBaseParameters());
            //Console.WriteLine("baseString: " + baseString);
            String signature = RsaSign(baseString);

            //Console.WriteLine("signature: " + signature);
            oAuthParameters.setOAuthSignature(signature);

            StringBuilder builder = new StringBuilder();

            foreach (KeyValuePair <string, string> entry in oAuthParameters.getBaseParameters())
            {
                if (builder.Length == 0)
                {
                    builder.Append(OAuthParameters.OAUTH_KEY).Append(" ");
                }
                else
                {
                    builder.Append(",");
                }
                builder.Append((entry.Key)).Append("=\"").Append(Util.UriRfc3986(entry.Value)).Append("\"");
            }


            //Console.WriteLine("authKey: " + builder.ToString());
            return(builder.ToString());
        }
コード例 #2
0
        /// <summary>
        /// This method generates an OAuth signature
        /// </summary>
        /// <param name="URL"></param>
        /// <param name="method"></param>
        /// <param name="body"></param>
        /// <returns></returns>
        public static String GenerateSignature(String URL, String method, String body, String clientId, AsymmetricAlgorithm privateKey)
        {
            OAuthParameters oAuthParameters = new OAuthParameters();

            oAuthParameters.setOAuthConsumerKey(clientId);
            oAuthParameters.setOAuthNonce(OAuthUtil.GetNonce());
            oAuthParameters.setOAuthTimestamp(OAuthUtil.GetTimestamp());
            oAuthParameters.setOAuthSignatureMethod("RSA-SHA1");
            oAuthParameters.setOAuthVersion("1.0");


            String encodedHash = Util.Base64Encode(Util.Sha1Encode(body));

            oAuthParameters.setOAuthBodyHash(encodedHash);


            String baseString = OAuthUtil.GetBaseString(URL, method, oAuthParameters.getBaseParameters());
            String signature  = RsaSign(baseString);

            oAuthParameters.setOAuthSignature(signature);

            StringBuilder builder = new StringBuilder();

            foreach (KeyValuePair <string, string> entry in oAuthParameters.getBaseParameters())
            {
                if (builder.Length == 0)
                {
                    builder.Append(OAuthParameters.OAUTH_KEY).Append(" ");
                }
                else
                {
                    builder.Append(",");
                }
                builder.Append((entry.Key)).Append("=\"").Append(Util.uriRfc3986(entry.Value)).Append("\"");
            }

            return(builder.ToString());
        }