private string GetSignature( Uri uri, string consumerSecret, string tokenSecret, string joinedParam, OAuthSigType sigType, string requestMethod) { switch (sigType) { case OAuthSigType.PlainText: return HttpUtility.UrlEncode(consumerSecret + "&" + tokenSecret); case OAuthSigType.Hmac_Sha1: if (String.IsNullOrEmpty(requestMethod)) throw new ArgumentNullException("httpMethod"); //URLのフォーマット var regularUrl = uri.Scheme + "://" + uri.Host; if (!((uri.Scheme == "http" && uri.Port == 80) || (uri.Scheme == "https" && uri.Port == 443))) regularUrl += ":" + uri.Port; regularUrl += uri.AbsolutePath; //シグネチャの生成 StringBuilder SigSource = new StringBuilder(); SigSource.Append(UrlEncode(requestMethod.ToUpper(), Encoding.UTF8, true) + "&"); SigSource.Append(UrlEncode(regularUrl, Encoding.UTF8, true) + "&"); SigSource.Append(UrlEncode(joinedParam, Encoding.UTF8, true)); //ハッシュの計算 using (HMACSHA1 hmacsha1 = new HMACSHA1()) { hmacsha1.Key = Encoding.ASCII.GetBytes(string.Format("{0}&{1}", UrlEncode(consumerSecret, Encoding.UTF8, true), string.IsNullOrEmpty(tokenSecret) ? "" : UrlEncode(tokenSecret, Encoding.UTF8, true))); return UrlEncode(ComputeHash(hmacsha1, SigSource.ToString()), Encoding.UTF8, false); } case OAuthSigType.Rsa_Sha1: throw new NotImplementedException(); default: throw new ArgumentException("Unknown signature type", "signatureType"); } }
private IEnumerable<KeyValuePair<string, string>> AddOAuthParams(IEnumerable<KeyValuePair<string, string>> origParam, string consumerKey, string token, string timeStamp, string nonce, OAuthSigType sigType, string verifier) { if (String.IsNullOrEmpty(consumerKey)) throw new ArgumentNullException("consumerKey"); var np = new List<KeyValuePair<string, string>>(); if (origParam != null) np.AddRange(origParam); np.Add(new KeyValuePair<string, string>(VersionKey, Version)); np.Add(new KeyValuePair<string, string>(NonceKey, nonce)); np.Add(new KeyValuePair<string, string>(TimestampKey, timeStamp)); np.Add(new KeyValuePair<string, string>(SignatureMethodKey, sigType.GetString())); np.Add(new KeyValuePair<string, string>(ConsumerKeyKey, consumerKey)); if (!String.IsNullOrEmpty(verifier)) np.Add(new KeyValuePair<string, string>(VerifierKey, verifier)); if (!String.IsNullOrEmpty(token)) np.Add(new KeyValuePair<string, string>(TokenKey, token)); return np; }