private HttpClient CreateHttpClientRequestHeaderMode(HttpMethodName methodName, String url, String token, String tokenSecret, IDictionary <String, String> queryString) { String timeStamp = OAuthClient.GenerateTimeStamp(); String nonce = OAuthClient.GenerateNonce(); Dictionary <String, String> pp = OAuthClient.GenerateParameters(ConsumerKey, token, timeStamp, nonce); Uri u = new Uri(HttpClient.CreateQueryString(url, queryString, OAuthClient.UrlEncode)); SignatureInfo si = GenerateSignature(u, this.ConsumerKey, this.ConsumerSecret, token, tokenSecret , methodName.ToString().ToUpper(), timeStamp, nonce); pp.Add("oauth_signature", OAuthClient.UrlEncode(si.Signature)); //NOTE:NET2.0 HttpClient cl = new HttpClient(HttpClient.CreateQueryString(url, queryString, HttpClient.UrlEncode)); cl.MethodName = methodName; cl.Headers[HttpRequestHeader.Authorization] = this.CreateOAuthHeader(pp); return(cl); }
/// <summary> /// /// </summary> /// <returns></returns> public AuthorizeInfo GetAuthorizeInfo() { String nonce = OAuthClient.GenerateNonce(); String timestamp = OAuthClient.GenerateTimeStamp(); String url = this.RequestTokenUrl; SignatureInfo si = OAuthClient.GenerateSignature(new Uri(url), this.ConsumerKey, this.ConsumerSecret , "", "", "GET", timestamp, nonce, OAuthClient.SignatureTypes.HMACSHA1); HttpClient cl = new HttpClient(String.Format("{0}?{1}&oauth_signature={2}", url, si.NormalizedRequestParameters, si.Signature)); String result = cl.GetBodyText(); //正規表現でoauth_token,oauth_token_secret取得 AuthorizeInfo ai = new AuthorizeInfo(); ai.AuthorizeUrl = String.Format("{0}?{1}", this.AuthorizeUrl, result); ai.RequestToken = this.GetMatchValue(RegexList.OAuthToken, result); ai.RequestTokenSecret = this.GetMatchValue(RegexList.OAuthTokenSecret, result); return(ai); }
/// <summary> /// Generates a signature using the specified signatureType /// </summary> /// <param name="url">The full url that needs to be signed including its non OAuth url parameters</param> /// <param name="consumerKey">The consumer key</param> /// <param name="consumerSecret">The consumer seceret</param> /// <param name="token">The token, if available. If not available pass null or an empty String</param> /// <param name="tokenSecret">The token secret, if available. If not available pass null or an empty String</param> /// <param name="httpMethod">The http method used. Must be a valid HTTP method verb (POST,GET,PUT, etc)</param> /// <param name="nonce"></param> /// <param name="signatureType">The type of signature to use</param> /// <param name="timeStamp"></param> /// <returns>A base64 String of the hash value</returns> public static SignatureInfo GenerateSignature(Uri url, String consumerKey, String consumerSecret, String token, String tokenSecret, String httpMethod, String timeStamp, String nonce, SignatureTypes signatureType) { SignatureInfo si = new SignatureInfo(); switch (signatureType) { case SignatureTypes.PLAINTEXT: si.Signature = OAuthClient.UrlEncode(String.Format("{0}&{1}", consumerSecret, tokenSecret)); return(si); case SignatureTypes.HMACSHA1: si = GenerateSignatureBase(url, consumerKey, token, httpMethod, timeStamp, nonce, Key.HMACSHA1SignatureType); HMACSHA1 hs = new HMACSHA1(); hs.Key = OAuthClient.GenerateSignatureEncoding.GetBytes(String.Format("{0}&{1}", OAuthClient.UrlEncode(consumerSecret), String.IsNullOrEmpty(tokenSecret) ? "" : OAuthClient.UrlEncode(tokenSecret))); si.Signature = GenerateSignatureUsingHash(si.Signature, hs); return(si); case SignatureTypes.RSASHA1: throw new NotImplementedException(); } throw new ArgumentException("Unknown signature type", "signatureType"); }
private HttpClient CreateHttpClientQueryStringMode(HttpMethodName methodName, String url, String token, String tokenSecret, IEnumerable <KeyValuePair <string, string> > queryString) { String timeStamp = OAuthClient.GenerateTimeStamp(); String nonce = OAuthClient.GenerateNonce(); Dictionary <String, String> pp = OAuthClient.GenerateParameters(ConsumerKey, token, timeStamp, nonce); foreach (KeyValuePair <string, string> p in queryString) { pp.Add(p.Key, p.Value); } //NOTE:NET2.0 Uri u = new Uri(HttpClient.CreateQueryString(url, pp, OAuthClient.UrlEncode)); SignatureInfo si = GenerateSignature(u, this.ConsumerKey, this.ConsumerSecret, token, tokenSecret , methodName.ToString().ToUpper(), timeStamp, nonce); pp.Add("oauth_signature", OAuthClient.UrlEncode(si.Signature)); //NOTE:NET2.0 HttpClient cl = new HttpClient(HttpClient.CreateQueryString(url, pp, HttpClient.UrlEncode)); cl.MethodName = methodName; return(cl); }