public string GenerateSignature(Uri url, string consumerKey, string consumerSecret, string token, string tokenSecret, string callBackUrl, string oauthVerifier, string httpMethod, string timeStamp, string nonce, TwitterBase.SignatureTypes signatureType, out string normalizedUrl, out string normalizedRequestParameters)
        {
            normalizedUrl = null;
            normalizedRequestParameters = null;
            string result;

            switch (signatureType)
            {
            case TwitterBase.SignatureTypes.HMACSHA1:
            {
                string signatureBase = this.GenerateSignatureBase(url, consumerKey, token, tokenSecret, callBackUrl, oauthVerifier, httpMethod, timeStamp, nonce, "HMAC-SHA1", out normalizedUrl, out normalizedRequestParameters);
                result = this.GenerateSignatureUsingHash(signatureBase, new HMACSHA1
                    {
                        Key = Encoding.ASCII.GetBytes(string.Format("{0}&{1}", TwitterBase.UrlEncode(consumerSecret), string.IsNullOrEmpty(tokenSecret) ? string.Empty : TwitterBase.UrlEncode(tokenSecret)))
                    });
                break;
            }

            case TwitterBase.SignatureTypes.PLAINTEXT:
                result = HttpUtility.UrlEncode(string.Format("{0}&{1}", consumerSecret, tokenSecret));
                break;

            case TwitterBase.SignatureTypes.RSASHA1:
                throw new NotImplementedException();

            default:
                throw new ArgumentException("Unknown signature type", "signatureType");
            }
            return(result);
        }
        public string GenerateSignatureBase(Uri url, string consumerKey, string token, string tokenSecret, string callBackUrl, string oauthVerifier, string httpMethod, string timeStamp, string nonce, string signatureType, out string normalizedUrl, out string normalizedRequestParameters)
        {
            if (token == null)
            {
                token = string.Empty;
            }
            if (tokenSecret == null)
            {
                tokenSecret = string.Empty;
            }
            if (string.IsNullOrEmpty(consumerKey))
            {
                throw new ArgumentNullException("consumerKey");
            }
            if (string.IsNullOrEmpty(httpMethod))
            {
                throw new ArgumentNullException("httpMethod");
            }
            if (string.IsNullOrEmpty(signatureType))
            {
                throw new ArgumentNullException("signatureType");
            }
            normalizedUrl = null;
            normalizedRequestParameters = null;
            List <TwitterBase.QueryParameter> queryParameters = this.GetQueryParameters(url.Query);

            queryParameters.Add(new TwitterBase.QueryParameter("oauth_version", "1.0"));
            queryParameters.Add(new TwitterBase.QueryParameter("oauth_nonce", nonce));
            queryParameters.Add(new TwitterBase.QueryParameter("oauth_timestamp", timeStamp));
            queryParameters.Add(new TwitterBase.QueryParameter("oauth_signature_method", signatureType));
            queryParameters.Add(new TwitterBase.QueryParameter("oauth_consumer_key", consumerKey));
            if (!string.IsNullOrEmpty(callBackUrl))
            {
                queryParameters.Add(new TwitterBase.QueryParameter("oauth_callback", TwitterBase.UrlEncode(callBackUrl)));
            }
            if (!string.IsNullOrEmpty(oauthVerifier))
            {
                queryParameters.Add(new TwitterBase.QueryParameter("oauth_verifier", oauthVerifier));
            }
            if (!string.IsNullOrEmpty(token))
            {
                queryParameters.Add(new TwitterBase.QueryParameter("oauth_token", token));
            }
            queryParameters.Sort(new TwitterBase.QueryParameterComparer());
            normalizedUrl = string.Format("{0}://{1}", url.Scheme, url.Host);
            if ((!(url.Scheme == "http") || url.Port != 80) && (!(url.Scheme == "https") || url.Port != 443))
            {
                normalizedUrl = normalizedUrl + ":" + url.Port;
            }
            normalizedUrl += url.AbsolutePath;
            normalizedRequestParameters = this.NormalizeRequestParameters(queryParameters);
            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.AppendFormat("{0}&", httpMethod.ToUpper());
            stringBuilder.AppendFormat("{0}&", TwitterBase.UrlEncode(normalizedUrl));
            stringBuilder.AppendFormat("{0}", TwitterBase.UrlEncode(normalizedRequestParameters));
            return(stringBuilder.ToString());
        }
        public string OAuthWebRequest(TwitterAuth.Method method, string url, string postData)
        {
            string str   = string.Empty;
            string text  = string.Empty;
            string empty = string.Empty;

            if (method == TwitterAuth.Method.POST || method == TwitterAuth.Method.DELETE || method == TwitterAuth.Method.GET)
            {
                if (postData.Length > 0)
                {
                    NameValueCollection nameValueCollection = HttpUtility.ParseQueryString(postData);
                    postData = string.Empty;
                    string[] allKeys = nameValueCollection.AllKeys;
                    for (int i = 0; i < allKeys.Length; i++)
                    {
                        string text2 = allKeys[i];
                        if (postData.Length > 0)
                        {
                            postData += "&";
                        }
                        nameValueCollection[text2] = HttpUtility.UrlDecode(nameValueCollection[text2]);
                        nameValueCollection[text2] = TwitterBase.UrlEncode(nameValueCollection[text2]);
                        postData = postData + text2 + "=" + nameValueCollection[text2];
                    }
                    if (url.IndexOf("?") > 0)
                    {
                        url += "&";
                    }
                    else
                    {
                        url += "?";
                    }
                    url += postData;
                }
            }
            Uri    url2      = new Uri(url);
            string nonce     = this.GenerateNonce();
            string timeStamp = this.GenerateTimeStamp();
            string value     = base.GenerateSignature(url2, this.ConsumerKey, this.ConsumerSecret, this.Token, this.TokenSecret, this.CallBackUrl, this.OAuthVerifier, method.ToString(), timeStamp, nonce, out str, out text);

            text = text + "&oauth_signature=" + TwitterBase.UrlEncode(value);
            if (method == TwitterAuth.Method.POST || method == TwitterAuth.Method.DELETE)
            {
                postData = text;
                text     = string.Empty;
            }
            if (text.Length > 0)
            {
                str += "?";
            }
            return(this.WebRequest(method, str + text, postData));
        }
Exemple #4
0
        public void Tweet(string content)
        {
            string      url         = string.Empty;
            string      text        = string.Empty;
            TwitterAuth twitterAuth = new TwitterAuth(TwitterConnect.API_Key, TwitterConnect.API_Secret, this.CallBackUrl);

            if (this.OAuthToken == null || this.OAuthTokenSecret == null)
            {
                twitterAuth.AccessTokenGet(HttpContext.Current.Request.QueryString["oauth_token"], HttpContext.Current.Request.QueryString["oauth_verifier"]);
                this.OAuthToken       = twitterAuth.TokenSecret;
                this.OAuthTokenSecret = twitterAuth.Token;
            }
            else
            {
                twitterAuth.TokenSecret   = this.OAuthToken;
                twitterAuth.Token         = this.OAuthTokenSecret;
                twitterAuth.OAuthVerifier = HttpContext.Current.Request.QueryString["oauth_verifier"];
            }
            if (twitterAuth.TokenSecret.Length > 0)
            {
                url  = "https://api.twitter.com/1.1/statuses/update.json";
                text = twitterAuth.OAuthWebRequest(TwitterAuth.Method.POST, url, "status=" + TwitterBase.UrlEncode(content));
            }
        }