Esempio n. 1
0
        /// <summary>
        /// Reinitialize the list of ids of the users blocked by the current user if required by the boolean parameter.
        /// Retrieve information about the users blocked by the current user from the Twitter API.
        /// </summary>
        /// <param name="blockedUsersQuery">Request to send to the Twitter API</param>
        /// <param name="del">delegate that will handle the data sent in the Twitter API's response</param>
        /// <param name="createBlockedUsersIds">True by default. Update the attribute _blocked_users_ids if this parameter is true</param>
        /// <returns>Null if there is no valid token for the current user. Otherwise, The list of ids of the users blocked by the current user.</returns>
        private void executeBlockedUsersQuery(string blockedUsersQuery,
                                              DynamicResponseDelegate del,
                                              bool createBlockedUsersIds = true)
        {
            // Reset the list of blocked users' ids if required
            if (createBlockedUsersIds)
            {
                _blocked_users_ids = null;
                _blocked_users_ids = new List <long>();
            }

            WebExceptionHandlingDelegate wexDel = delegate(WebException wex)
            {
                int?wexStatusNumber = ExceptionUtils.GetWebExceptionStatusNumber(wex);
                switch (wexStatusNumber)
                {
                // Handle the Exception when the user's token is not valid
                case 401:
                    throw new WebException("Blocked users can only be accessed using the user's token. Any other token is rejected.", wex);

                default:
                    throw wex;
                }
            };

            _token.ExecuteCursorQuery(blockedUsersQuery, 0, Int32.MaxValue, del, wexDel);
        }
Esempio n. 2
0
        /// <summary>
        /// Retrieve the timeline of the current user from the Twitter API.
        /// Update the corresponding attribute if required by the parameter createTimeline.
        /// Return the timeline of the current user
        /// </summary>
        /// <returns>Null if there is no user token, the timeline of the current user otherwise</returns>
        public List <ITweet> GetUserTimeline(bool createUserTimeline = false, IToken token = null)
        {
            // Handle the possible exceptions thrown by the Twitter API
            WebExceptionHandlingDelegate wexDel = delegate(WebException wex)
            {
                // Get the exception status number
                int?wexStatusNumber = ExceptionUtils.GetWebExceptionStatusNumber(wex);
                if (wexStatusNumber == null)
                {
                    throw wex;
                }

                switch (wexStatusNumber)
                {
                case 400:
                    //Rate limit reached. Throw a new Exception with a specific message
                    throw new WebException("Rate limit is reached", wex);

                default:
                    //Throw the exception "as-is"
                    throw wex;
                }
            };

            return(GetUserTimeline(_token, wexDel, createUserTimeline));
        }
Esempio n. 3
0
        /// <summary>
        /// Request the list of direct message from Twitter using the token and the URL given in parameter
        /// Handle the exception when the token represents a user who did not authorize the application to get the direct messages
        /// </summary>
        /// <param name="token"></param>
        /// <param name="url"></param>
        /// <param name="directMessageDelegate"></param>
        /// <returns>The list of messages retrieved from the Twitter API</returns>
        private List <IMessage> GetDirectMessages(IToken token,
                                                  string url,
                                                  ObjectCreatedDelegate <IMessage> directMessageDelegate = null)
        {
            token = GetQueryToken(token);

            if (token == null || url == null)
            {
                throw new ArgumentException("token or request url is null");
            }

            WebExceptionHandlingDelegate webExceptionDelegate = delegate(WebException wex)
            {
                int?wexStatusNumber = ExceptionUtils.GetWebExceptionStatusNumber(wex);
                if (wexStatusNumber == null)
                {
                    throw wex;
                }

                switch (wexStatusNumber)
                {
                case 403:
                    Exception e = new Exception("Not enough permission to access direct messages. " +
                                                "Update the application access level and ask the user to reauthorize it.");
                    throw e;

                default:
                    throw wex;
                }
            };

            return(ResultGenerator.GetMessages(token, url, directMessageDelegate, webExceptionDelegate));
        }
        public IToken GenerateToken(
            string twitterConfirmationCode,
            string authorizationKey,
            string authorizationSecret,
            string consumerKey,
            string consumerSecret)
        {
            IOAuthCredentials credentials = new OAuthCredentials(authorizationKey,
                                                                 authorizationSecret,
                                                                 consumerKey,
                                                                 consumerSecret);

            IOAuthToken token = new OAuthToken(credentials);

            List <IOAuthQueryParameter> headers = token.GenerateParameters().ToList();

            headers.Add(new OAuthQueryParameter("oauth_verifier", twitterConfirmationCode, true, true, false));

            string response = "";

            WebExceptionHandlingDelegate wex = delegate(WebException exception)
            {
                if (ExceptionUtils.GetWebExceptionStatusNumber(exception) == 401)
                {
                    response = null;
                }
            };

            response = token.ExecuteQueryWithSpecificParameters(Resources.OAuthRequestAccessToken,
                                                                HttpMethod.POST,
                                                                wex,
                                                                headers);

            if (response == null)
            {
                return(null);
            }

            Match responseInformation = Regex.Match(response, Resources.OAuthTokenAccessRegex);

            IToken result = null;

            if (responseInformation.Groups["oauth_token"] != null &&
                responseInformation.Groups["oauth_token_secret"] != null)
            {
                result = new Token(responseInformation.Groups["oauth_token"].Value,
                                   responseInformation.Groups["oauth_token_secret"].Value,
                                   consumerKey,
                                   consumerSecret);
            }

            return(result);
        }
Esempio n. 5
0
        /// <summary>
        /// Get the list of contributors to the account of the current user
        /// Update the matching attribute of the current user if the parameter is true
        /// Return the list of contributors
        /// </summary>
        /// <param name="createContributorList">False by default. Indicates if the _contributors attribute needs to be updated with the result</param>
        /// <returns>The list of contributors to the account of the current user</returns>
        public List <IUser> GetContributors(bool createContributorList = false)
        {
            // Specific error handler
            // Manage the error 400 thrown when contributors are not enabled by the current user
            WebExceptionHandlingDelegate del = delegate(WebException wex)
            {
                int?wexStatusNumber = ExceptionUtils.GetWebExceptionStatusNumber(wex);
                if (wexStatusNumber != null)
                {
                    switch (wexStatusNumber)
                    {
                    case 400:
                        // Don't need to do anything, the method will return null
                        Console.WriteLine("Contributors are not enabled for this user");
                        break;

                    default:
                        // Other errors are not managed
                        throw wex;
                    }
                }
                else
                {
                    throw wex;
                }
            };

            List <IUser> result = null;

            // Contributors can be researched according to the user's id or screen_name
            if (this.Id != null)
            {
                result = GetContributionObjects(_token, String.Format(Resources.User_GetContributorsFromId, this.Id), del);
            }
            else if (this.ScreenName != null)
            {
                result = GetContributionObjects(_token, String.Format(Resources.User_GetContributorsFromScreenName, this.ScreenName), del);
            }

            // Update the attribute _contributors if required
            if (createContributorList)
            {
                _contributors = result;
            }

            return(result);
        }
Esempio n. 6
0
        /// <summary>
        /// Get the list of contributors to the account of the current user
        /// Update the matching attribute of the current user if the parameter is true
        /// Return the list of contributors
        /// </summary>
        /// <param name="createContributorList">False by default. Indicates if the _contributors attribute needs to be updated with the result</param>
        /// <returns>The list of contributors to the account of the current user</returns>
        public List <IUser> GetContributors(bool createContributorList = false)
        {
            // Specific error handler
            // Manage the error 400 thrown when contributors are not enabled by the current user
            WebExceptionHandlingDelegate del = delegate(WebException wex)
            {
                int?wexStatusNumber = ExceptionUtils.GetWebExceptionStatusNumber(wex);
                if (wexStatusNumber != null)
                {
                    switch (wexStatusNumber)
                    {
                    case 400:
                        // Don't need to do anything, the method will return null
                        Console.WriteLine("Contributors are not enabled for this user");
                        break;

                    default:
                        // Other errors are not managed
                        throw wex;
                    }
                }
                else
                {
                    throw wex;
                }
            };

            string query = Resources.User_GetContributors;

            if (!AddUserInformationInQuery(ref query))
            {
                return(null);
            }

            List <IUser> result = GetContributionObjects(_token, query, del);

            if (createContributorList)
            {
                _contributors = result;
            }

            return(result);
        }
Esempio n. 7
0
        public List <IMention> GetLatestMentionsTimeline(int count = 20)
        {
            string query = String.Format(Resources.TokenUser_GetLatestMentionTimeline, count);

            WebExceptionHandlingDelegate wexDelegate = delegate(WebException wex)
            {
                int?wexStatusNumber = ExceptionUtils.GetWebExceptionStatusNumber(wex);
                switch (wexStatusNumber)
                {
                case 400:
                    throw new WebException("Wrong token for user id = " + this.Id, wex);

                default:
                    throw wex;
                }
            };

            LatestMentionsTimeline = ResultGenerator.GetMentions(_token, query, null, wexDelegate);
            return(LatestMentionsTimeline);
        }