Search code examples
c#tweetinvi

TweetInvi - Cannot Unfollow Users


I have the following code that is intended to find the users you are following, and the users that you have requested to follow. It then unfollows the one's that are not following you. However it never actually seem's to work, however the AuthenticatedUser_UnFollowUser returns true. Any ideas? Thanks.

            private void AuthenticateUser()
                    {
                        CheckRateLimits();
                        Auth.SetUserCredentials(_consumerKey, _consumerSecret, _userAccessToken, _userAccessSecret);
                        authenticatedUser = User.GetAuthenticatedUser();
                    }

                    private void CheckRateLimits()
                    {

                        // Enable RateLimit Tracking
                        RateLimit.RateLimitTrackerMode = RateLimitTrackerMode.TrackAndAwait;
                        TweetinviEvents.QueryBeforeExecute += (sender, args) =>
                        {
                            var queryRateLimits = RateLimit.GetQueryRateLimit(args.QueryURL);

                            // Some methods are not RateLimited. Invoking such a method will result in the queryRateLimits to be null
                            if (queryRateLimits != null)
                            {
                                if (queryRateLimits.Remaining > 0)
                                {
                                    AppendProgress("RateLimits Available : " + queryRateLimits.Remaining.ToString());
                                    // We have enough resource to execute the query
                                    return;
                                }

                                // Strategy #1 : Wait for RateLimits to be available
                                AppendProgress("Waiting for RateLimits until : " + queryRateLimits.ResetDateTime.ToLongTimeString() + "For " + queryRateLimits.ToString());
                                MessageBox.Show("Waiting for " + queryRateLimits.ResetDateTime.ToLongTimeString());
                                Thread.Sleep((int)queryRateLimits.ResetDateTimeInMilliseconds);

                                // Strategy #2 : Use different credentials
                                //var alternateCredentials = TwitterCredentials.CreateCredentials("", "", "", "");
                                //var twitterQuery = args.TwitterQuery;
                                //twitterQuery.OAuthCredentials = alternateCredentials;

                                // Strategy #3 : Cancel Query
                                //args.Cancel = true;
                            }
                        };
                    }

                  private void UnfollowUsersNotFollowingYou()
                    {
                        AuthenticateUser();
                        var toUnfollow = Examples.Friendship_GetUsersNotFollowingYou();

                        toUnfollow.ForEach(x =>
                        {
                            if (Examples.AuthenticatedUser_UnFollowUser(x.ScreenName))
                            {
                                AppendProgress("You have unfollowed " + x.ScreenName);
                                SaveUnfollowedUserIdToTextFile(x.ScreenName);
                            }
                        });

                    }
//From Examples Static Class                
                public static bool AuthenticatedUser_UnFollowUser(string userName)
                    {
                        var authenticatedUser = User.GetAuthenticatedUser();
                        var userToFollow = User.GetUserFromScreenName(userName);

                        bool unfollowed = authenticatedUser.UnFollowUser(userToFollow);

                        return unfollowed;
                    }

    //Users Not Following You
                public static IEnumerable<IUser> Friendship_GetUsersNotFollowingYou()
            {
                var currentuser = Examples.User_GetCurrentUserScreenname();
                var followers = Examples.User_GetFriendIds(currentuser);
                var following = Examples.Friendship_GetUsersYouRequestedToFollow();

                var toUnfollow = following.Where(x => followers != x.FriendIds);

                return toUnfollow;
            }

Solution

  • I am the developer of Tweetinvi. I have just verified and unfollow like follow seems to work properly. Could you please verify that you are using the latest version of Tweetinvi (0.9.13.0)?

    Also please note that Unfollow will return success even if the user was not followed. This is what Twitter is returning and there is nothing I can do about that.

    EDIT :

    The problem came from the fact that @david-beamont was not retrieving the users properly.

    Here is the code to get the following users :

    var authenticatedUser = User.GetAuthenticatedUser(); 
    var friends = authenticatedUser.GetFriends();