예제 #1
0
		private static string ConvertToString(AccessToken token){
			var text = token.TokenValue + "&" + token.TokenSecret;
			return Protect(text, optionalEntropy);
		}
예제 #2
0
        /// <summary>
        /// Access protected resource with access token
        /// </summary>
        /// <param name="accessToken">Access token</param>
        /// <param name="urlString">URL string for accessing protected resource</param>
        /// <param name="method">HTTP method to access</param>
        /// <param name="authorizationRealm">realm for accessing protected resource</param>
        /// <param name="queryParameters">Query parameter to be sent</param>
        /// <param name="additionalParameters">Parameters added to Authorization header</param>
        /// <returns>HttpWebResponse from protected resource</returns>
        public void AccessProtectedResource(
			AccessToken accessToken,
			HttpWebRequest req,
			string urlString,
			string authorizationRealm,
			Parameter[] queryParameters,
			Parameter[] additionalParameters)
        {
            if(additionalParameters == null)
                additionalParameters = new Parameter[0];

            if(queryParameters == null)
                queryParameters = new Parameter[0];

            if(accessToken == null)
                accessToken = new AccessToken("", "");

            string oauth_consumer_key = _consumerKey;
            string oauth_token = accessToken.TokenValue;
            string oauth_signature_method = "HMAC-SHA1";
            string oauth_timestamp =
                ((DateTime.UtcNow.Ticks - new DateTime(1970, 1, 1).Ticks) / (1000 * 10000)).ToString();
            string oauth_nonce =
                Guid.NewGuid().ToString();
            #if !SILVERLIGHT
            req.Timeout = this.Timeout;

            //Twitter service does not accept expect100continue
            req.ServicePoint.Expect100Continue = false;
            #endif
            string oauth_signature =
                CreateHMACSHA1Signature(
                    req.Method,
                    urlString,
                    (new Parameter[]{
                        new Parameter("oauth_consumer_key",oauth_consumer_key),
                        new Parameter("oauth_token",oauth_token ),
                        new Parameter ("oauth_signature_method",oauth_signature_method ),
                        new Parameter ("oauth_timestamp",oauth_timestamp),
                        new Parameter ("oauth_nonce",oauth_nonce )
                    }).Concat(additionalParameters).Concat(queryParameters),
                    _consumerSecret,
                    accessToken.TokenSecret
                );

            req.Headers.Add(
                "Authorization: OAuth " +
                "realm=\"" + authorizationRealm + "\"," +
                "oauth_consumer_key=\"" + Uri.EscapeDataString(oauth_consumer_key) + "\"," +
                "oauth_token=\"" + Uri.EscapeDataString(oauth_token) + "\"," +
                "oauth_signature_method=\"" + Uri.EscapeDataString(oauth_signature_method) + "\"," +
                "oauth_signature=\"" + Uri.EscapeDataString(oauth_signature) + "\"," +
                "oauth_timestamp=\"" + Uri.EscapeDataString(oauth_timestamp) + "\"," +
                "oauth_nonce=\"" + Uri.EscapeDataString(oauth_nonce) + "\"" +
                (additionalParameters.Length > 0 ? "," + additionalParameters.EncodeQuery("\"") : "")
            );
        }
예제 #3
0
        /// <summary>
        /// Access protected resource with access token
        /// </summary>
        /// <param name="accessToken">Access token</param>
        /// <param name="urlString">URL string for accessing protected resource</param>
        /// <param name="method">HTTP method to access</param>
        /// <param name="authorizationRealm">realm for accessing protected resource</param>
        /// <param name="queryParameters">Query parameter to be sent</param>
        /// <returns>HttpWebResponse from protected resource</returns>
        public void AccessProtectedResource(
			AccessToken accessToken,
			HttpWebRequest req,
			string urlString,
			string authorizationRealm,
			Parameter[] queryParameters
		)
        {
            AccessProtectedResource(
                accessToken,
                req,
                urlString,
                authorizationRealm,
                queryParameters,
                null);
        }
예제 #4
0
 public PostingWebRequest DestroyList(ulong id, AccessToken token)
 {
     const string url = "http://api.twitter.com/1/lists/destroy.xml";
     return Post(url, new Parameter[]{new Parameter("list_id", id.ToString())}, token);
 }
예제 #5
0
 public PostingWebRequest DestroyStatus(AccessToken token, ulong id)
 {
     const string url = "http://api.twitter.com/1/statuses/destroy";
     Parameter[] prms = new Parameter[1]{new Parameter("id", id.ToString())};
     return Post(url + "/" + id.ToString() + ".xml", prms, token);
 }
예제 #6
0
        protected PostingWebRequest Post(string url, Parameter[] prms, AccessToken token)
        {
            string query = prms.EncodeQuery();
            #if !SILVERLIGHT
            byte[] data = Encoding.ASCII.GetBytes(query);
            #else
            byte[] data = StringToAscii(query);
            #endif

            HttpWebRequest req = GetWebRequest(url, prms);
            req.Method = WebRequestMethods.Http.Post;
            req.ContentType = "application/x-www-form-urlencoded";
            req.ContentLength = query.Length;

            Consumer.AccessProtectedResource(token, req, url, "http://twitter.com/", prms);
            return new PostingWebRequest(req, data);
        }
예제 #7
0
 public PostingWebRequest CreateFriendship(AccessToken token, ulong id)
 {
     const string url = "http://api.twitter.com/1/friendships/create";
     Parameter[] prms = new Parameter[1]{new Parameter("id", id.ToString())};
     return Post(url + "/" + id.ToString() + ".xml", prms, token);
 }
예제 #8
0
 public GettingWebRequest VerifyCredential(AccessToken token)
 {
     const string url = "https://api.twitter.com/1/account/verify_credentials.xml";
     return Get(url, new Parameter[0], token);
 }
예제 #9
0
        protected GettingWebRequest Get(string url, Parameter[] prms, AccessToken token)
        {
            HttpWebRequest req = GetWebRequest(url + ((prms.Length > 0) ? ("?" + prms.EncodeQuery()) : ""), prms);
            req.Method = WebRequestMethods.Http.Get;

            Consumer.AccessProtectedResource(token, req, url, "http://twitter.com/", prms);
            return new GettingWebRequest(req);
        }
예제 #10
0
 public GettingWebRequest SearchUsers(AccessToken token, string searchWord, int count, int page)
 {
     const string url = "http://api.twitter.com/1/users/search.xml";
     List<Parameter> prms = new List<Parameter>();
     prms.Add(new Parameter("q", searchWord));
     if(count > 0){
         prms.Add(new Parameter("per_page", count.ToString()));
     }
     if(page > 0){
         prms.Add(new Parameter("page", page.ToString()));
     }
     return Get(url, prms.ToArray(), token);
 }
예제 #11
0
 public PostingWebRequest UpdateStatus(AccessToken token, string status, ulong replyTo, string source)
 {
     const string url = "http://api.twitter.com/1/statuses/update.xml";
     List<Parameter> prms = new List<Parameter>();
     prms.Add(new Parameter("status", status));
     prms.Add(new Parameter("source", source));
     if(replyTo > 0){
         prms.Add(new Parameter("in_reply_to_status_id", replyTo.ToString()));
     }
     return Post(url, prms.ToArray(), token);
 }
예제 #12
0
 public PostingWebRequest Retweet(AccessToken token, ulong id)
 {
     const string url = "http://api.twitter.com/1/statuses/retweet/";
     return Post(url + id.ToString() + ".xml", new Parameter[0], token);
 }
예제 #13
0
 public GettingWebRequest GetHomeTimeline(AccessToken token, int count, int page, ulong sinceId, ulong maxId, bool trimUser)
 {
     const string url = "http://api.twitter.com/1/statuses/home_timeline.xml";
     List<Parameter> prms = new List<Parameter>();
     if(count > 0){
         prms.Add(new Parameter("count", count.ToString()));
     }
     if(page > 0){
         prms.Add(new Parameter("page", page.ToString()));
     }
     if(sinceId > 0){
         prms.Add(new Parameter("since_id", sinceId.ToString()));
     }
     if(maxId > 0){
         prms.Add(new Parameter("max_id", maxId.ToString()));
     }
     if(trimUser){
         prms.Add(new Parameter("trim_user", "1"));
     }
     return Get(url, prms.ToArray(), token);
 }
예제 #14
0
 public GettingWebRequest GetFriends(AccessToken token, ulong user_id, long cursor)
 {
     const string url = "http://api.twitter.com/1/friends/ids.xml";
     return Get(url, new Parameter[]{new Parameter("user_id", user_id.ToString()), new Parameter("cursor", cursor.ToString())}, token);
 }
예제 #15
0
 public GettingWebRequest GetFollowers(AccessToken token, ulong user_id)
 {
     const string url = "http://api.twitter.com/1/followers/ids.xml";
     return Get(url, new Parameter[]{new Parameter("user_id", user_id.ToString())}, token);
 }
예제 #16
0
 public GettingWebRequest GetBlocks(AccessToken token, int page)
 {
     const string url = "http://api.twitter.com/1/blocks/blocking.xml";
     return Get(url, new Parameter[]{new Parameter("page", page.ToString())}, token);
 }