Пример #1
0
        public TumblrUserInfo GetUserInfo(TumblrAccessToken token)
        {
            string method = "GET";

            string oAuthSignatureMethod = "HMAC-SHA1";

            string oAuthNonce = Guid.NewGuid().ToString().Replace("-", string.Empty);
            string oAuthTimestamp = UnixTime.UnixTimeStamp().ToString();

            string parameters = "oauth_consumer_key=" + consumerKey + "&oauth_nonce=" + oAuthNonce + "&oauth_signature_method=" + oAuthSignatureMethod + "&oauth_timestamp=" + oAuthTimestamp + "&oauth_token=" + UrlEncode(token.Token) + "&oauth_version=1.0";

            string twitterEndpoint = "http://api.tumblr.com/v2/user/info";

            string signature = method + "&" + UrlEncode(twitterEndpoint) + "&" + UrlEncode(parameters);

            String oauthSignature = string.Empty;
            try
            {
                oauthSignature = computeSignature(signature, consumerSecret + "&" + token.Secret);
            }
            catch (Exception ex)
            {
            }

            string authorisationHeader = "OAuth oauth_consumer_key=\"" + consumerKey + "\",oauth_signature_method=\"HMAC-SHA1\",oauth_timestamp=\"" +
                oAuthTimestamp + "\",oauth_nonce=\"" + oAuthNonce + "\",oauth_version=\"1.0\",oauth_signature=\"" + UrlEncode(oauthSignature) + "\",oauth_token=\"" + UrlEncode(token.Token) + "\"";

            HttpWebRequest wr = (HttpWebRequest)HttpWebRequest.Create(twitterEndpoint);
            wr.ProtocolVersion = HttpVersion.Version11;
            wr.UserAgent = "HttpCore/1.1";
            wr.Method = method;
            wr.Headers["Authorization"] = authorisationHeader;

            HttpWebResponse response = (HttpWebResponse)wr.GetResponse();

            if (response.StatusCode != HttpStatusCode.OK)
            {
            }
            else
            {
                Encoding encode = Encoding.GetEncoding("utf-8");
                StreamReader sr = new StreamReader(response.GetResponseStream(), encode);

                string responseString = sr.ReadToEnd();

                return new TumblrUserInfo(responseString);
            }

            return null;
        }
Пример #2
0
        public TumblrPost StatusesUpdate(TumblrAccessToken token, string hostname, ActionableItemType type, string title, string post, string link, byte[] data, string dataContentType)
        {
            string method = "POST";

            string oAuthSignatureMethod = "HMAC-SHA1";

            string oAuthNonce = Guid.NewGuid().ToString().Replace("-", string.Empty);
            string oAuthTimestamp = UnixTime.UnixTimeStamp().ToString();

            string postType = string.Empty;
            string parameters = string.Empty;
            switch (type)
            {
                case ActionableItemType.Photo:
                    postType = "photo";
                    parameters = "caption=" + UrlEncode(post) + "&format=html&link=" + UrlEncode(link) + "&oauth_consumer_key=" + consumerKey + "&oauth_nonce=" + oAuthNonce + "&oauth_signature_method=" + oAuthSignatureMethod + "&oauth_timestamp=" + oAuthTimestamp + "&oauth_token=" + UrlEncode(token.Token) + "&oauth_version=1.0&type=" + postType;
                    break;
                case ActionableItemType.Audio:
                    postType = "audio";
                    parameters = "caption=" + UrlEncode(post) + "&format=html&oauth_consumer_key=" + consumerKey + "&oauth_nonce=" + oAuthNonce + "&oauth_signature_method=" + oAuthSignatureMethod + "&oauth_timestamp=" + oAuthTimestamp + "&oauth_token=" + UrlEncode(token.Token) + "&oauth_version=1.0&type=" + postType;
                    break;
                case ActionableItemType.Video:
                    postType = "video";
                    break;
                default:
                    postType = "text";
                    parameters = "body=" + UrlEncode(post) + "&format=html&oauth_consumer_key=" + consumerKey + "&oauth_nonce=" + oAuthNonce + "&oauth_signature_method=" + oAuthSignatureMethod + "&oauth_timestamp=" + oAuthTimestamp + "&oauth_token=" + UrlEncode(token.Token) + "&oauth_version=1.0&type=" + postType;
                    break;
            }

            string twitterEndpoint = string.Format("http://api.tumblr.com/v2/blog/{0}/post", hostname);

            string signature = method + "&" + UrlEncode(twitterEndpoint) + "&" + UrlEncode(parameters);

            String oauthSignature = string.Empty;
            try
            {
                oauthSignature = computeSignature(signature, consumerSecret + "&" + token.Secret);
            }
            catch (Exception)
            {
            }

            string authorisationHeader = "OAuth oauth_consumer_key=\"" + consumerKey + "\",oauth_signature_method=\"HMAC-SHA1\",oauth_timestamp=\"" +
                oAuthTimestamp + "\",oauth_nonce=\"" + oAuthNonce + "\",oauth_version=\"1.0\",oauth_signature=\"" + UrlEncode(oauthSignature) + "\",oauth_token=\"" + UrlEncode(token.Token) + "\"";

            string guid = Guid.NewGuid().ToString();
            string boundary = "----BSFB" + UnixTime.UnixTimeStamp().ToString() + guid.Replace("-", string.Empty);
            StringBuilder body = new StringBuilder();

            HttpWebRequest wr = (HttpWebRequest)HttpWebRequest.Create(twitterEndpoint);
            wr.ProtocolVersion = HttpVersion.Version11;
            wr.UserAgent = "HttpCore/1.1";
            wr.Method = method;
            wr.Headers["Authorization"] = authorisationHeader;
            if (data == null)
            {
                body.Append("type=" + postType + "&format=html&body=" + UrlEncode(post));

                wr.ContentType = "application/x-www-form-urlencoded";
            }
            else
            {
                string extension = "";
                switch (dataContentType)
                {
                    case "image/jpeg":
                        extension = ".jpg";
                        break;
                    case "image/gif":
                        extension = ".gif";
                        break;
                    case "image/png":
                        extension = ".png";
                        break;
                }

                body.Append("--" + boundary + "\r\n");
                body.Append("Content-Disposition: form-data; name=\"type\"\r\n\r\n");
                body.Append(postType + "\r\n");

                body.Append("--" + boundary + "\r\n");
                body.Append("Content-Disposition: form-data; name=\"format\"\r\n\r\n");
                body.Append("html\r\n");

                body.Append("--" + boundary + "\r\n");
                body.Append("Content-Disposition: form-data; name=\"link\"\r\n\r\n");
                body.Append(link + "\r\n");

                body.Append("--" + boundary + "\r\n");
                body.Append("Content-Disposition: form-data; name=\"caption\"\r\n\r\n");
                body.Append(post + "\r\n");

                body.Append("--" + boundary + "\r\n");
                body.Append("Content-Disposition: form-data; name=\"data[0]\"; filename=\"" + guid + extension + "\"\r\n");
                body.Append("Content-Type: " + dataContentType + "\r\n\r\n");

                wr.ContentType = "multipart/form-data; boundary=" + boundary;
            }

            byte[] bodyBytes = UTF8Encoding.UTF8.GetBytes(body.ToString());
            byte[] boundaryBytes = UTF8Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n");

            if (data == null)
            {
                wr.ContentLength = bodyBytes.Length;
            }
            else
            {
                wr.ContentLength = bodyBytes.Length + data.Length + boundaryBytes.Length;
            }

            Stream stream = wr.GetRequestStream();
            stream.Write(bodyBytes, 0, bodyBytes.Length);
            if (data != null)
            {
                stream.Write(data, 0, data.Length);
                stream.Write(boundaryBytes, 0, boundaryBytes.Length);
            }

            stream.Close();

            HttpWebResponse response = (HttpWebResponse)wr.GetResponse();

            if (response.StatusCode != HttpStatusCode.OK && response.StatusCode != HttpStatusCode.Created)
            {
            }
            else
            {
                Encoding encode = Encoding.GetEncoding("utf-8");
                StreamReader sr = new StreamReader(response.GetResponseStream(), encode);

                string responseString = sr.ReadToEnd();

                return new TumblrPost(responseString);
            }

            return null;
        }
Пример #3
0
        public void DeleteStatus(TumblrAccessToken token, string hostname, long postId)
        {
            string method = "POST";

            string oAuthSignatureMethod = "HMAC-SHA1";

            string oAuthNonce = Guid.NewGuid().ToString().Replace("-", string.Empty);
            string oAuthTimestamp = UnixTime.UnixTimeStamp().ToString();

            string parameters = "id=" + postId.ToString() + "&oauth_consumer_key=" + consumerKey + "&oauth_nonce=" + oAuthNonce + "&oauth_signature_method=" + oAuthSignatureMethod + "&oauth_timestamp=" + oAuthTimestamp + "&oauth_token=" + UrlEncode(token.Token) + "&oauth_version=1.0";

            string twitterEndpoint = string.Format("http://api.tumblr.com/v2/blog/{0}/post/delete", hostname);

            string signature = method + "&" + UrlEncode(twitterEndpoint) + "&" + UrlEncode(parameters);

            String oauthSignature = string.Empty;
            try
            {
                oauthSignature = computeSignature(signature, consumerSecret + "&" + token.Secret);
            }
            catch (Exception ex)
            {
            }

            string authorisationHeader = "OAuth oauth_consumer_key=\"" + consumerKey + "\",oauth_signature_method=\"HMAC-SHA1\",oauth_timestamp=\"" +
                oAuthTimestamp + "\",oauth_nonce=\"" + oAuthNonce + "\",oauth_version=\"1.0\",oauth_signature=\"" + UrlEncode(oauthSignature) + "\",oauth_token=\"" + UrlEncode(token.Token) + "\"";

            string body = "id=" + postId.ToString();

            HttpWebRequest wr = (HttpWebRequest)HttpWebRequest.Create(twitterEndpoint);
            wr.ProtocolVersion = HttpVersion.Version11;
            wr.UserAgent = "HttpCore/1.1";
            wr.ContentType = "application/x-www-form-urlencoded";
            wr.Method = method;
            wr.Headers["Authorization"] = authorisationHeader;
            wr.ContentLength = body.Length;

            byte[] bodyBytes = UTF8Encoding.UTF8.GetBytes(body);

            Stream stream = wr.GetRequestStream();
            stream.Write(bodyBytes, 0, bodyBytes.Length);

            HttpWebResponse response = (HttpWebResponse)wr.GetResponse();

            if (response.StatusCode != HttpStatusCode.OK)
            {
            }
            else
            {
                // ignore response
            }
        }