1 // OpenTween - Client of Twitter
2 // Copyright (c) 2007-2011 kiri_feather (@kiri_feather) <kiri.feather@gmail.com>
3 // (c) 2008-2011 Moz (@syo68k)
4 // (c) 2008-2011 takeshik (@takeshik) <http://www.takeshik.org/>
5 // (c) 2010-2011 anis774 (@anis774) <http://d.hatena.ne.jp/anis774/>
6 // (c) 2010-2011 fantasticswallow (@f_swallow) <http://twitter.com/f_swallow>
7 // (c) 2011 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
8 // All rights reserved.
10 // This file is part of OpenTween.
12 // This program is free software; you can redistribute it and/or modify it
13 // under the terms of the GNU General public License as published by the Free
14 // Software Foundation; either version 3 of the License, or (at your option)
17 // This program is distributed in the hope that it will be useful, but
18 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General public License
22 // You should have received a copy of the GNU General public License along
23 // with this program. if (not, see <http://www.gnu.org/licenses/>, or write to
24 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
25 // Boston, MA 02110-1301, USA.
28 using System.Collections.Generic;
36 public class HttpTwitter : ICloneable
42 /// 旧APIが使用出来なくなったら消す予定。
43 /// 静的フィールドとしているのは TwitterUserstream クラスが Clone メソッドを使用しているため
45 public static bool API11Enabled { get; set; }
49 ///OAuthのアクセストークン取得先URI
51 private const string AccessTokenUrlXAuth = "https://api.twitter.com/oauth/access_token";
52 private const string RequestTokenUrl = "https://api.twitter.com/oauth/request_token";
53 private const string AuthorizeUrl = "https://api.twitter.com/oauth/authorize";
54 private const string AccessTokenUrl = "https://api.twitter.com/oauth/access_token";
56 private static string _protocol = "http://";
58 private const string PostMethod = "POST";
59 private const string GetMethod = "GET";
61 private IHttpConnection httpCon; //HttpConnectionApi or HttpConnectionOAuth
62 private HttpVarious httpConVar = new HttpVarious();
64 private enum AuthMethod
69 private AuthMethod connectionType = AuthMethod.Basic;
71 private string requestToken;
73 private static string tk = "";
74 private static string tks = "";
75 private static string un = "";
77 private Dictionary<string, string> apiStatusHeaders = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
79 {"X-Access-Level", ""},
80 {"X-RateLimit-Limit", ""},
81 {"X-RateLimit-Remaining", ""},
82 {"X-RateLimit-Reset", ""},
83 {"X-Rate-Limit-Limit", ""},
84 {"X-Rate-Limit-Remaining", ""},
85 {"X-Rate-Limit-Reset", ""},
86 {"X-MediaRateLimit-Limit", ""},
87 {"X-MediaRateLimit-Remaining", ""},
88 {"X-MediaRateLimit-Reset", ""},
93 HttpTwitter.API11Enabled = true;
96 public void Initialize(string accessToken,
97 string accessTokenSecret,
102 HttpOAuthApiProxy con = new HttpOAuthApiProxy();
103 if (tk != accessToken || tks != accessTokenSecret ||
104 un != username || connectionType != AuthMethod.OAuth)
106 // 以前の認証状態よりひとつでも変化があったらhttpヘッダより読み取ったカウントは初期化
108 tks = accessTokenSecret;
111 con.Initialize(ApplicationSettings.TwitterConsumerKey, ApplicationSettings.TwitterConsumerSecret, accessToken, accessTokenSecret, username, userId, "screen_name", "user_id");
113 connectionType = AuthMethod.OAuth;
117 public string AccessToken
122 return ((HttpConnectionOAuth)httpCon).AccessToken;
128 public string AccessTokenSecret
133 return ((HttpConnectionOAuth)httpCon).AccessTokenSecret;
139 public string AuthenticatedUsername
144 return httpCon.AuthUsername;
150 public long AuthenticatedUserId
155 return httpCon.AuthUserId;
162 httpCon.AuthUserId = value;
166 public string Password
174 public bool AuthGetRequestToken(ref string content)
177 bool result = ((HttpOAuthApiProxy)httpCon).AuthenticatePinFlowRequest(RequestTokenUrl, AuthorizeUrl, ref requestToken, ref authUri);
178 content = authUri.ToString();
182 public HttpStatusCode AuthGetAccessToken(string pin)
184 return ((HttpOAuthApiProxy)httpCon).AuthenticatePinFlow(AccessTokenUrl, requestToken, pin);
187 public HttpStatusCode AuthUserAndPass(string username, string password, ref string content)
189 return httpCon.Authenticate(new Uri(AccessTokenUrlXAuth), username, password, ref content);
192 public void ClearAuthInfo()
194 this.Initialize("", "", "", 0);
197 public static bool UseSsl
202 _protocol = "https://";
204 _protocol = "http://";
208 public HttpStatusCode UpdateStatus(string status, long replyToId, ref string content)
210 Dictionary<string, string> param = new Dictionary<string, string>();
211 param.Add("status", status);
212 if (replyToId > 0) param.Add("in_reply_to_status_id", replyToId.ToString());
213 param.Add("include_entities", "true");
214 //if (AppendSettingDialog.Instance.ShortenTco && AppendSettingDialog.Instance.UrlConvertAuto) param.Add("wrap_links", "true")
216 return httpCon.GetContent(PostMethod,
217 CreateTwitterUri(HttpTwitter.API11Enabled ? "/1.1/statuses/update.json" : "/1/statuses/update.json"),
224 public HttpStatusCode UpdateStatusWithMedia(string status, long replyToId, FileInfo mediaFile, ref string content)
227 Dictionary<string, string> param = new Dictionary<string, string>();
228 param.Add("status", status);
229 if (replyToId > 0) param.Add("in_reply_to_status_id", replyToId.ToString());
230 param.Add("include_entities", "true");
231 //if (AppendSettingDialog.Instance.ShortenTco && AppendSettingDialog.Instance.UrlConvertAuto) param.Add("wrap_links", "true")
233 List<KeyValuePair<string, FileInfo>> binary = new List<KeyValuePair<string, FileInfo>>();
234 binary.Add(new KeyValuePair<string, FileInfo>("media[]", mediaFile));
236 return httpCon.GetContent(PostMethod,
237 HttpTwitter.API11Enabled ? CreateTwitterUri("/1.1/statuses/update_with_media.json") : new Uri("https://upload.twitter.com/1/statuses/update_with_media.json"),
241 this.apiStatusHeaders,
242 HttpTwitter.API11Enabled ? CreateApi11Calllback("/statuses/update_with_media") : GetApiCallback);
245 public HttpStatusCode DestroyStatus(long id)
247 string content = null;
249 return httpCon.GetContent(PostMethod,
250 CreateTwitterUri(HttpTwitter.API11Enabled ? "/1.1/statuses/destroy/" + id + ".json" : "/1/statuses/destroy/" + id + ".json"),
257 public HttpStatusCode SendDirectMessage(string status, string sendto, ref string content)
259 Dictionary<string, string> param = new Dictionary<string, string>();
260 param.Add("text", status);
261 param.Add("screen_name", sendto);
262 //if (AppendSettingDialog.Instance.ShortenTco && AppendSettingDialog.Instance.UrlConvertAuto) param.Add("wrap_links", "true")
264 return httpCon.GetContent(PostMethod,
265 CreateTwitterUri(HttpTwitter.API11Enabled ? "/1.1/direct_messages/new.json" : "/1/direct_messages/new.json"),
272 public HttpStatusCode DestroyDirectMessage(long id)
274 string content = null;
276 var param = new Dictionary<string, string>();
277 if (HttpTwitter.API11Enabled)
278 param.Add("id", id.ToString());
280 return httpCon.GetContent(PostMethod,
281 CreateTwitterUri(HttpTwitter.API11Enabled ? "/1.1/direct_messages/destroy.json" : "/1/direct_messages/destroy/" + id + ".json"),
288 public HttpStatusCode RetweetStatus(long id, ref string content)
290 Dictionary<string, string> param = new Dictionary<string, string>();
291 param.Add("include_entities", "true");
293 return httpCon.GetContent(PostMethod,
294 CreateTwitterUri(HttpTwitter.API11Enabled ? "/1.1/statuses/retweet/" + id + ".json" : "/1/statuses/retweet/" + id + ".json"),
301 public HttpStatusCode ShowUserInfo(string screenName, ref string content)
303 Dictionary<string, string> param = new Dictionary<string, string>();
304 param.Add("screen_name", screenName);
305 param.Add("include_entities", "true");
306 return httpCon.GetContent(GetMethod,
307 CreateTwitterUri(HttpTwitter.API11Enabled ? "/1.1/users/show.json" : "/1/users/show.json"),
310 this.apiStatusHeaders,
311 HttpTwitter.API11Enabled ? CreateApi11Calllback("/users/show/:id") : GetApiCallback);
314 public HttpStatusCode CreateFriendships(string screenName, ref string content)
316 Dictionary<string, string> param = new Dictionary<string, string>();
317 param.Add("screen_name", screenName);
319 return httpCon.GetContent(PostMethod,
320 CreateTwitterUri(HttpTwitter.API11Enabled ? "/1.1/friendships/create.json" : "/1/friendships/create.json"),
327 public HttpStatusCode DestroyFriendships(string screenName, ref string content)
329 Dictionary<string, string> param = new Dictionary<string, string>();
330 param.Add("screen_name", screenName);
332 return httpCon.GetContent(PostMethod,
333 CreateTwitterUri(HttpTwitter.API11Enabled ? "/1.1/friendships/destroy.json" : "/1/friendships/destroy.json"),
340 public HttpStatusCode CreateBlock(string screenName, ref string content)
342 Dictionary<string, string> param = new Dictionary<string, string>();
343 param.Add("screen_name", screenName);
345 return httpCon.GetContent(PostMethod,
346 CreateTwitterUri(HttpTwitter.API11Enabled ? "/1.1/blocks/create.json" : "/1/blocks/create.json"),
353 public HttpStatusCode DestroyBlock(string screenName, ref string content)
355 Dictionary<string, string> param = new Dictionary<string, string>();
356 param.Add("screen_name", screenName);
358 return httpCon.GetContent(PostMethod,
359 CreateTwitterUri(HttpTwitter.API11Enabled ? "/1.1/blocks/destroy.json" : "/1/blocks/destroy.json"),
366 public HttpStatusCode ReportSpam(string screenName, ref string content)
368 Dictionary<string, string> param = new Dictionary<string, string>();
369 param.Add("screen_name", screenName);
371 return httpCon.GetContent(PostMethod,
372 CreateTwitterUri(HttpTwitter.API11Enabled ? "/1.1/users/report_spam.json" : "/1/report_spam.json"),
379 public HttpStatusCode ShowFriendships(string souceScreenName, string targetScreenName, ref string content)
381 Dictionary<string, string> param = new Dictionary<string, string>();
382 param.Add("source_screen_name", souceScreenName);
383 param.Add("target_screen_name", targetScreenName);
385 return httpCon.GetContent(GetMethod,
386 CreateTwitterUri(HttpTwitter.API11Enabled ? "/1.1/friendships/show.json" : "/1/friendships/show.json"),
389 this.apiStatusHeaders,
390 HttpTwitter.API11Enabled ? CreateApi11Calllback("/friendships/show") : GetApiCallback);
393 public HttpStatusCode ShowStatuses(long id, ref string content)
395 Dictionary<string, string> param = new Dictionary<string, string>();
396 param.Add("include_entities", "true");
397 return httpCon.GetContent(GetMethod,
398 CreateTwitterUri(HttpTwitter.API11Enabled ? "/1.1/statuses/show/" + id + ".json" : "/1/statuses/show/" + id + ".json"),
401 this.apiStatusHeaders,
402 HttpTwitter.API11Enabled ? CreateApi11Calllback("/statuses/show/:id") : GetApiCallback);
405 public HttpStatusCode CreateFavorites(long id, ref string content)
407 var param = new Dictionary<string, string>();
408 if (HttpTwitter.API11Enabled)
409 param.Add("id", id.ToString());
411 return httpCon.GetContent(PostMethod,
412 CreateTwitterUri(HttpTwitter.API11Enabled ? "/1.1/favorites/create.json" : "/1/favorites/create/" + id + ".json"),
419 public HttpStatusCode DestroyFavorites(long id, ref string content)
421 var param = new Dictionary<string, string>();
422 if (HttpTwitter.API11Enabled)
423 param.Add("id", id.ToString());
425 return httpCon.GetContent(PostMethod,
426 CreateTwitterUri(HttpTwitter.API11Enabled ? "/1.1/favorites/destroy.json" : "/1/favorites/destroy/" + id + ".json"),
433 public HttpStatusCode HomeTimeline(int count, long max_id, long since_id, ref string content)
435 Dictionary<string, string> param = new Dictionary<string, string>();
437 param.Add("count", count.ToString());
439 param.Add("max_id", max_id.ToString());
441 param.Add("since_id", since_id.ToString());
443 param.Add("include_entities", "true");
445 return httpCon.GetContent(GetMethod,
446 CreateTwitterUri(HttpTwitter.API11Enabled ? "/1.1/statuses/home_timeline.json" : "/1/statuses/home_timeline.json"),
449 this.apiStatusHeaders,
450 HttpTwitter.API11Enabled ? CreateApi11Calllback("/statuses/home_timeline") : GetApiCallback);
453 public HttpStatusCode UserTimeline(long user_id, string screen_name, int count, long max_id, long since_id, ref string content)
455 Dictionary<string, string> param = new Dictionary<string, string>();
457 if ((user_id == 0 && string.IsNullOrEmpty(screen_name)) ||
458 (user_id != 0 && !string.IsNullOrEmpty(screen_name))) return HttpStatusCode.BadRequest;
461 param.Add("user_id", user_id.ToString());
462 if (!string.IsNullOrEmpty(screen_name))
463 param.Add("screen_name", screen_name);
465 param.Add("count", count.ToString());
467 param.Add("max_id", max_id.ToString());
469 param.Add("since_id", since_id.ToString());
471 param.Add("include_rts", "true");
472 param.Add("include_entities", "true");
474 return httpCon.GetContent(GetMethod,
475 CreateTwitterUri(HttpTwitter.API11Enabled ? "/1.1/statuses/user_timeline.json" : "/1/statuses/user_timeline.json"),
478 this.apiStatusHeaders,
479 HttpTwitter.API11Enabled ? CreateApi11Calllback("/statuses/user_timeline") : GetApiCallback);
482 public HttpStatusCode PublicTimeline(int count, long max_id, long since_id, ref string content)
484 Dictionary<string, string> param = new Dictionary<string, string>();
486 param.Add("count", count.ToString());
488 param.Add("max_id", max_id.ToString());
490 param.Add("since_id", since_id.ToString());
492 param.Add("include_entities", "true");
494 // TODO: API v1.1 に存在しない API (旧 API で代替)
496 return httpCon.GetContent(GetMethod,
497 CreateTwitterUri("/1/statuses/public_timeline.json"),
500 this.apiStatusHeaders,
504 public HttpStatusCode Mentions(int count, long max_id, long since_id, ref string content)
506 Dictionary<string, string> param = new Dictionary<string, string>();
508 param.Add("count", count.ToString());
510 param.Add("max_id", max_id.ToString());
512 param.Add("since_id", since_id.ToString());
514 param.Add("include_entities", "true");
516 return httpCon.GetContent(GetMethod,
517 CreateTwitterUri(HttpTwitter.API11Enabled ? "/1.1/statuses/mentions_timeline.json" : "/1/statuses/mentions.json"),
520 this.apiStatusHeaders,
521 HttpTwitter.API11Enabled ? CreateApi11Calllback("/statuses/mentions_timeline") : GetApiCallback);
524 public HttpStatusCode DirectMessages(int count, long max_id, long since_id, ref string content)
526 Dictionary<string, string> param = new Dictionary<string, string>();
528 param.Add("count", count.ToString());
530 param.Add("max_id", max_id.ToString());
532 param.Add("since_id", since_id.ToString());
533 param.Add("include_entities", "true");
535 return httpCon.GetContent(GetMethod,
536 CreateTwitterUri(HttpTwitter.API11Enabled ? "/1.1/direct_messages.json" : "/1/direct_messages.json"),
539 this.apiStatusHeaders,
540 HttpTwitter.API11Enabled ? CreateApi11Calllback("/direct_messages") : GetApiCallback);
543 public HttpStatusCode DirectMessagesSent(int count, long max_id, long since_id, ref string content)
545 Dictionary<string, string> param = new Dictionary<string, string>();
547 param.Add("count", count.ToString());
549 param.Add("max_id", max_id.ToString());
551 param.Add("since_id", since_id.ToString());
552 param.Add("include_entities", "true");
554 return httpCon.GetContent(GetMethod,
555 CreateTwitterUri(HttpTwitter.API11Enabled ? "/1.1/direct_messages/sent.json" : "/1/direct_messages/sent.json"),
558 this.apiStatusHeaders,
559 HttpTwitter.API11Enabled ? CreateApi11Calllback("/direct_messages/sent") : GetApiCallback);
562 public HttpStatusCode Favorites(int count, int page, ref string content)
564 Dictionary<string, string> param = new Dictionary<string, string>();
565 if (count != 20) param.Add("count", count.ToString());
569 param.Add("page", page.ToString());
572 param.Add("include_entities", "true");
574 return httpCon.GetContent(GetMethod,
575 CreateTwitterUri(HttpTwitter.API11Enabled ? "/1.1/favorites/list.json" : "/1/favorites.json"),
578 this.apiStatusHeaders,
579 HttpTwitter.API11Enabled ? CreateApi11Calllback("/favorites/list") : GetApiCallback);
582 public HttpStatusCode PhoenixSearch(string querystr, ref string content)
584 Dictionary<string, string> param = new Dictionary<string, string>();
587 if (string.IsNullOrEmpty(querystr)) return HttpStatusCode.BadRequest;
589 tmp = querystr.Split(new char[] {'?', '&'}, StringSplitOptions.RemoveEmptyEntries);
590 foreach (string tmp2 in tmp)
592 paramstr = tmp2.Split(new char[] {'='});
593 param.Add(paramstr[0], paramstr[1]);
596 return httpConVar.GetContent(GetMethod,
597 CreateTwitterUri("/phoenix_search.phoenix"),
601 MyCommon.GetAssemblyName());
604 public HttpStatusCode PhoenixSearch(string words, string lang, int rpp, int page, long sinceId, ref string content)
606 Dictionary<string, string> param = new Dictionary<string, string>();
607 if (!string.IsNullOrEmpty(words)) param.Add("q", words);
608 param.Add("include_entities", "1");
609 param.Add("contributor_details", "true");
610 if (!string.IsNullOrEmpty(lang)) param.Add("lang", lang);
611 if (rpp > 0) param.Add("rpp", rpp.ToString());
612 if (page > 0) param.Add("page", page.ToString());
613 if (sinceId > 0) param.Add("since_id", sinceId.ToString());
615 if (param.Count == 0) return HttpStatusCode.BadRequest;
617 return httpConVar.GetContent(GetMethod,
618 CreateTwitterUri("/phoenix_search.phoenix"),
622 MyCommon.GetAssemblyName());
625 public HttpStatusCode Search(string words, string lang, int count, int page, long sinceId, ref string content)
627 Dictionary<string, string> param = new Dictionary<string, string>();
628 if (!string.IsNullOrEmpty(words)) param.Add("q", words);
629 if (!string.IsNullOrEmpty(lang)) param.Add("lang", lang);
630 if (count > 0) param.Add(HttpTwitter.API11Enabled ? "count" : "rpp", count.ToString());
631 if (page > 0) param.Add("page", page.ToString());
632 if (sinceId > 0) param.Add("since_id", sinceId.ToString());
634 if (param.Count == 0) return HttpStatusCode.BadRequest;
636 param.Add("result_type", "recent");
637 param.Add("include_entities", "true");
638 return httpCon.GetContent(GetMethod,
639 HttpTwitter.API11Enabled ? this.CreateTwitterUri("/1.1/search/tweets.json") : this.CreateTwitterSearchUri("/search.json"),
643 HttpTwitter.API11Enabled ? CreateApi11Calllback("/search/tweets") : GetApiCallback);
646 public HttpStatusCode SavedSearches(ref string content)
648 return httpCon.GetContent(GetMethod,
649 CreateTwitterUri(HttpTwitter.API11Enabled ? "/1.1/saved_searches/list.json" : "/1/saved_searches.json"),
653 HttpTwitter.API11Enabled ? CreateApi11Calllback("/saved_searches/list") : GetApiCallback);
656 public HttpStatusCode FollowerIds(long cursor, ref string content)
658 Dictionary<string, string> param = new Dictionary<string, string>();
659 param.Add("cursor", cursor.ToString());
661 return httpCon.GetContent(GetMethod,
662 CreateTwitterUri(HttpTwitter.API11Enabled ? "/1.1/followers/ids.json" : "/1/followers/ids.json"),
665 this.apiStatusHeaders,
666 HttpTwitter.API11Enabled ? CreateApi11Calllback("/followers/ids") : GetApiCallback);
669 public HttpStatusCode NoRetweetIds(long cursor, ref string content)
671 Dictionary<string, string> param = new Dictionary<string, string>();
672 param.Add("cursor", cursor.ToString());
674 return httpCon.GetContent(GetMethod,
675 CreateTwitterUri(HttpTwitter.API11Enabled ? "/1.1/friendships/no_retweets/ids.json" : "/1/friendships/no_retweet_ids.json"),
678 this.apiStatusHeaders,
679 HttpTwitter.API11Enabled ? CreateApi11Calllback("/friendships/no_retweets/ids") : GetApiCallback);
682 public HttpStatusCode RateLimitStatus(ref string content)
684 return httpCon.GetContent(GetMethod,
685 CreateTwitterUri(HttpTwitter.API11Enabled ? "/1.1/application/rate_limit_status.json" : "/1/account/rate_limit_status.json"),
688 this.apiStatusHeaders,
689 HttpTwitter.API11Enabled ? CreateApi11Calllback("/application/rate_limit_status") : GetApiCallback);
693 public HttpStatusCode GetLists(string user, long? cursor, ref string content)
695 Dictionary<string, string> param = new Dictionary<string, string>();
696 param.Add("screen_name", user);
699 param.Add("cursor", cursor.Value.ToString()); // API v1
701 return httpCon.GetContent(GetMethod,
702 CreateTwitterUri(HttpTwitter.API11Enabled ? "/1.1/lists/list.json" : "/1/lists.json"),
705 this.apiStatusHeaders,
706 HttpTwitter.API11Enabled ? CreateApi11Calllback("/lists/list") : GetApiCallback);
709 public HttpStatusCode UpdateListID(string user, string list_id, string name, Boolean isPrivate, string description, ref string content)
711 string mode = "public";
715 Dictionary<string, string> param = new Dictionary<string, string>();
716 param.Add("screen_name", user);
717 param.Add("list_id", list_id);
718 if (name != null) param.Add("name", name);
719 if (mode != null) param.Add("mode", mode);
720 if (description != null) param.Add("description", description);
722 return httpCon.GetContent(PostMethod,
723 CreateTwitterUri(HttpTwitter.API11Enabled ? "/1.1/lists/update.json" : "/1/lists/update.json"),
730 public HttpStatusCode DeleteListID(string user, string list_id, ref string content)
732 Dictionary<string, string> param = new Dictionary<string, string>();
733 param.Add("screen_name", user);
734 param.Add("list_id", list_id);
736 return httpCon.GetContent(PostMethod,
737 CreateTwitterUri(HttpTwitter.API11Enabled ? "/1.1/lists/destroy.json" : "/1/lists/destroy.json"),
744 public HttpStatusCode GetListsSubscriptions(string user, long? cursor, ref string content)
746 Dictionary<string, string> param = new Dictionary<string, string>();
747 param.Add("screen_name", user);
750 param.Add("cursor", cursor.Value.ToString()); // API v1
752 return httpCon.GetContent(GetMethod,
753 CreateTwitterUri(HttpTwitter.API11Enabled ? "/1.1/lists/subscriptions.json" : "/1/lists/subscriptions.json"),
756 this.apiStatusHeaders,
757 HttpTwitter.API11Enabled ? CreateApi11Calllback("/lists/subscriptions") : GetApiCallback);
760 public HttpStatusCode GetListsStatuses(long userId, long list_id, int per_page, long max_id, long since_id, Boolean isRTinclude, ref string content)
762 //認証なくても取得できるが、protectedユーザー分が抜ける
763 Dictionary<string, string> param = new Dictionary<string, string>();
764 param.Add("user_id", userId.ToString());
765 param.Add("list_id", list_id.ToString());
766 param.Add("include_rts", isRTinclude ? "true" : "false");
768 param.Add("per_page", per_page.ToString());
770 param.Add("max_id", max_id.ToString());
772 param.Add("since_id", since_id.ToString());
773 param.Add("include_entities", "true");
775 return httpCon.GetContent(GetMethod,
776 CreateTwitterUri(HttpTwitter.API11Enabled ? "/1.1/lists/statuses.json" : "/1/lists/statuses.json"),
779 this.apiStatusHeaders,
780 HttpTwitter.API11Enabled ? CreateApi11Calllback("/lists/statuses") : GetApiCallback);
783 public HttpStatusCode CreateLists(string listname, Boolean isPrivate, string description, ref string content)
785 string mode = "public";
789 Dictionary<string, string> param = new Dictionary<string, string>();
790 param.Add("name", listname);
791 param.Add("mode", mode);
792 if (!string.IsNullOrEmpty(description))
793 param.Add("description", description);
795 return httpCon.GetContent(PostMethod,
796 CreateTwitterUri(HttpTwitter.API11Enabled ? "/1.1/lists/create.json" : "/1/lists/create.json"),
803 public HttpStatusCode GetListMembers(string user, string list_id, long cursor, ref string content)
805 Dictionary<string, string> param = new Dictionary<string, string>();
806 param.Add("screen_name", user);
807 param.Add("list_id", list_id);
808 param.Add("cursor", cursor.ToString());
809 return httpCon.GetContent(GetMethod,
810 CreateTwitterUri(HttpTwitter.API11Enabled ? "/1.1/lists/members.json" : "/1/lists/members.json"),
813 this.apiStatusHeaders,
814 HttpTwitter.API11Enabled ? CreateApi11Calllback("/lists/members") : GetApiCallback);
817 public HttpStatusCode CreateListMembers(string list_id, string memberName, ref string content)
819 Dictionary<string, string> param = new Dictionary<string, string>();
820 param.Add("list_id", list_id);
821 param.Add("screen_name", memberName);
822 return httpCon.GetContent(PostMethod,
823 CreateTwitterUri(HttpTwitter.API11Enabled ? "/1.1/lists/members/create.json" : "/1/lists/members/create.json"),
830 //public HttpStatusCode CreateListMembers(string user, string list_id, string memberName, ref string content)
832 // //正常に動かないので旧APIで様子見
833 // //Dictionary<string, string> param = new Dictionary<string, string>();
834 // //param.Add("screen_name", user)
835 // //param.Add("list_id", list_id)
836 // //param.Add("member_screen_name", memberName)
837 // //return httpCon.GetContent(PostMethod,
838 // // CreateTwitterUri("/1/lists/members/create.json"),
843 // Dictionary<string, string> param = new Dictionary<string, string>();
844 // param.Add("id", memberName)
845 // return httpCon.GetContent(PostMethod,
846 // CreateTwitterUri("/1/" + user + "/" + list_id + "/members.json"),
853 public HttpStatusCode DeleteListMembers(string list_id, string memberName, ref string content)
855 Dictionary<string, string> param = new Dictionary<string, string>();
856 param.Add("screen_name", memberName);
857 param.Add("list_id", list_id);
858 return httpCon.GetContent(PostMethod,
859 CreateTwitterUri(HttpTwitter.API11Enabled ? "/1.1/lists/members/destroy.json" : "/1/lists/members/destroy.json"),
866 //public HttpStatusCode DeleteListMembers(string user, string list_id, string memberName, ref string content)
868 // //Dictionary<string, string> param = new Dictionary<string, string>();
869 // //param.Add("screen_name", user)
870 // //param.Add("list_id", list_id)
871 // //param.Add("member_screen_name", memberName)
872 // //return httpCon.GetContent(PostMethod,
873 // // CreateTwitterUri("/1/lists/members/destroy.json"),
878 // Dictionary<string, string> param = new Dictionary<string, string>();
879 // param.Add("id", memberName)
880 // param.Add("_method", "DELETE")
881 // return httpCon.GetContent(PostMethod,
882 // CreateTwitterUri("/1/" + user + "/" + list_id + "/members.json"),
889 public HttpStatusCode ShowListMember(string list_id, string memberName, ref string content)
891 //新APIがmember_screen_nameもmember_user_idも無視して、自分のIDを返してくる。
892 //正式にドキュメントに反映されるまで旧APIを使用する
893 Dictionary<string, string> param = new Dictionary<string, string>();
894 param.Add("screen_name", memberName);
895 param.Add("list_id", list_id);
896 return httpCon.GetContent(GetMethod,
897 CreateTwitterUri(HttpTwitter.API11Enabled ? "/1.1/lists/members/show.json" : "/1/lists/members/show.json"),
900 this.apiStatusHeaders,
901 HttpTwitter.API11Enabled ? CreateApi11Calllback("/lists/members/show") : GetApiCallback);
905 public HttpStatusCode Statusid_retweeted_by_ids(long statusid, int count, int page, ref string content)
907 Dictionary<string, string> param = new Dictionary<string, string>();
909 param.Add("count", count.ToString());
911 param.Add("page", page.ToString());
913 if (HttpTwitter.API11Enabled)
914 param.Add("id", statusid.ToString());
916 return httpCon.GetContent(GetMethod,
917 CreateTwitterUri(HttpTwitter.API11Enabled ? "/1.1/statuses/retweeters/ids.json" : "/1/statuses/" + statusid + "/retweeted_by/ids.json"),
920 this.apiStatusHeaders,
921 HttpTwitter.API11Enabled ? CreateApi11Calllback("/statuses/retweeters/ids") : GetApiCallback);
924 public HttpStatusCode UpdateProfile(string name, string url, string location, string description, ref string content)
926 Dictionary<string, string> param = new Dictionary<string, string>();
928 param.Add("name", WebUtility.HtmlEncode(name));
929 param.Add("url", url);
930 param.Add("location", WebUtility.HtmlEncode(location));
931 param.Add("description", WebUtility.HtmlEncode(description));
932 param.Add("include_entities", "true");
934 return httpCon.GetContent(PostMethod,
935 CreateTwitterUri(HttpTwitter.API11Enabled ? "/1.1/account/update_profile.json" : "/1/account/update_profile.json"),
942 public HttpStatusCode UpdateProfileImage(FileInfo imageFile, ref string content)
944 List<KeyValuePair<string, FileInfo>> binary = new List<KeyValuePair<string, FileInfo>>();
945 binary.Add(new KeyValuePair<string, FileInfo>("image", imageFile));
947 return httpCon.GetContent(PostMethod,
948 CreateTwitterUri(HttpTwitter.API11Enabled ? "/1.1/account/update_profile_image.json" : "/1/account/update_profile_image.json"),
956 public HttpStatusCode GetRelatedResults(long id, ref string content)
958 //認証なくても取得できるが、protectedユーザー分が抜ける
959 Dictionary<string, string> param = new Dictionary<string, string>();
961 param.Add("include_entities", "true");
963 // TODO: API v1.1 に存在しない API (旧 API で代替)
965 return httpCon.GetContent(GetMethod,
966 CreateTwitterUri("/1/related_results/show/" + id + ".json"),
969 this.apiStatusHeaders,
973 public HttpStatusCode GetBlockUserIds(ref string content)
975 return httpCon.GetContent(GetMethod,
976 CreateTwitterUri(HttpTwitter.API11Enabled ? "/1.1/blocks/ids.json" : "/1/blocks/blocking/ids.json"),
979 this.apiStatusHeaders,
980 HttpTwitter.API11Enabled ? CreateApi11Calllback("/blocks/ids") : GetApiCallback);
983 public HttpStatusCode GetConfiguration(ref string content)
985 return httpCon.GetContent(GetMethod,
986 CreateTwitterUri(HttpTwitter.API11Enabled ? "/1.1/help/configuration.json" : "/1/help/configuration.json"),
989 this.apiStatusHeaders,
990 HttpTwitter.API11Enabled ? CreateApi11Calllback("/help/configuration") : GetApiCallback);
993 public HttpStatusCode VerifyCredentials(ref string content)
995 return httpCon.GetContent(GetMethod,
996 CreateTwitterUri(HttpTwitter.API11Enabled ? "/1.1/account/verify_credentials.json" : "/1/account/verify_credentials.json"),
999 this.apiStatusHeaders,
1000 HttpTwitter.API11Enabled ? CreateApi11Calllback("/account/verify_credentials") : GetApiCallback);
1004 private static string _twitterUrl = "api.twitter.com";
1005 private static string _TwitterSearchUrl = "search.twitter.com";
1006 private static string _twitterUserStreamUrl = "userstream.twitter.com";
1007 private static string _twitterStreamUrl = "stream.twitter.com";
1009 private Uri CreateTwitterUri(string path)
1011 return new Uri(string.Format("{0}{1}{2}", _protocol, _twitterUrl, path));
1014 private Uri CreateTwitterSearchUri(string path)
1016 return new Uri(string.Format("{0}{1}{2}", _protocol, _TwitterSearchUrl, path));
1019 private Uri CreateTwitterUserStreamUri(string path)
1021 return new Uri(string.Format("{0}{1}{2}", "https://", _twitterUserStreamUrl, path));
1024 private Uri CreateTwitterStreamUri(string path)
1026 return new Uri(string.Format("{0}{1}{2}", "http://", _twitterStreamUrl, path));
1029 public static string TwitterUrl
1033 _twitterUrl = value;
1034 HttpOAuthApiProxy.ProxyHost = value;
1038 public static string TwitterSearchUrl
1042 _TwitterSearchUrl = value;
1047 private void GetApiCallback(Object sender, HttpStatusCode code, string content)
1049 if (code < HttpStatusCode.InternalServerError)
1050 MyCommon.TwitterApiInfo.UpdateFromHeader(this.apiStatusHeaders);
1053 private CallbackDelegate CreateApi11Calllback(string endpointName)
1055 return (sender, code, content) =>
1057 if (code < HttpStatusCode.InternalServerError)
1058 MyCommon.TwitterApiInfo11.UpdateFromHeader(this.apiStatusHeaders, endpointName);
1062 public HttpStatusCode UserStream(ref Stream content,
1067 Dictionary<string, string> param = new Dictionary<string, string>();
1070 param.Add("replies", "all");
1072 if (!string.IsNullOrEmpty(trackwords))
1073 param.Add("track", trackwords);
1075 return httpCon.GetContent(GetMethod,
1076 CreateTwitterUserStreamUri(HttpTwitter.API11Enabled ? "/1.1/user.json" : "/2/user.json"),
1082 public HttpStatusCode FilterStream(ref Stream content,
1086 //文中の日本語キーワードに反応せず、使えない(スペースで分かち書きしてないと反応しない)
1087 Dictionary<string, string> param = new Dictionary<string, string>();
1089 if (!string.IsNullOrEmpty(trackwords))
1090 param.Add("track", string.Join(",", trackwords.Split(" ".ToCharArray())));
1092 return httpCon.GetContent(PostMethod,
1093 CreateTwitterStreamUri(HttpTwitter.API11Enabled ? "/1.1/statuses/filter.json" : "/1/statuses/filter.json"),
1099 public void RequestAbort()
1101 httpCon.RequestAbort();
1104 public object Clone()
1106 HttpTwitter myCopy = new HttpTwitter();
1107 myCopy.Initialize(this.AccessToken, this.AccessTokenSecret, this.AuthenticatedUsername, this.AuthenticatedUserId);