Example of Follow/Unfollow

Mar 13, 2014 at 11:56 AM
Would it be possible to see some sample code demonstrating Following and Unfollowing another twitter user.

Thanks a lot.
Mar 13, 2014 at 1:40 PM
Edited Mar 13, 2014 at 1:40 PM
I believe I have found this information in

Thanks for this.
Mar 13, 2014 at 1:46 PM

Yes you can find all the examples in Program.
For the sake of developers coming to this post here is the code:
var loggedUser = User.GetLoggedUser();
var userToFollow = User.GetUserFromScreenName(userName);

if (loggedUser.FollowUser(userToFollow))
        Console.WriteLine("You have successfully sent a request to follow {0}", userToFollow.Name);
Marked as answer by linvi on 3/13/2014 at 6:46 AM
Apr 4, 2014 at 3:05 AM
Edited Apr 4, 2014 at 9:19 PM
I try this solution with the next code:

private void BtnAceptar_Click(object sender, EventArgs e)
        TwitterCredentials.SetCredentials("UserAccesToken", "UserAccesSecret", "ConsumerKey", "ConsumerSecret");
        var usuario = User.GetUserFromScreenName(TxtUsuario.Text);
        var Seguidores = usuario.GetFollowers();
        var YoUsuario = User.GetLoggedUser();

        foreach (var Seguidor in Seguidores)
            var userToFollow = User.GetUserFromScreenName(Seguidor.ScreenName);
but I've the next exception in the line ... YoUsuario.FollowUser(userToFollow):

No se controló System.ArgumentNullException
Message=El valor no puede ser nulo.
Nombre del parámetro: s
   en System.IO.StringReader..ctor(String s)
   en Newtonsoft.Json.Linq.JObject.Parse(String json)
   en TweetinviLogic.Wrapper.JObjectStaticWrapper.GetJobjectFromJson(String json)
   en TweetinviCredentials.TwitterAccessor.ExecutePOSTQuery(String query)
   en TweetinviCredentials.TwitterAccessor.TryExecutePOSTQuery(String query, JsonConverter[] converters)
   en TweetinviControllers.Friendship.FriendshipQueryExecutor.CreateFriendshipWith(IUserIdDTO userDTO)
   en TweetinviControllers.Friendship.FriendshipController.CreateFriendshipWith(IUserIdDTO userDTO)
   en TweetinviControllers.Friendship.FriendshipController.CreateFriendshipWith(IUser user)
   en TweetinviLogic.LoggedUser.<>c__DisplayClassf.<FollowUser>b__e()
   en TweetinviLogic.LoggedUser.StartLoggedUserOperation[T](Func`1 operation)
   en TweetinviLogic.LoggedUser.FollowUser(IUser user)
   en ConsigueSeguidores.Form1.BtnAceptar_Click(Object sender, EventArgs e) en e:\Mis documentos\Visual Studio Projects\ConsigueSeguidores\ConsigueSeguidores\Form1.cs:línea 31
   en System.Windows.Forms.Control.OnClick(EventArgs e)
   en System.Windows.Forms.Button.OnClick(EventArgs e)
   en System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   en System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   en System.Windows.Forms.Control.WndProc(Message& m)
   en System.Windows.Forms.ButtonBase.WndProc(Message& m)
   en System.Windows.Forms.Button.WndProc(Message& m)
   en System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   en System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   en System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   en System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   en System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   en System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   en System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   en System.Windows.Forms.Application.Run(Form mainForm)
   en ConsigueSeguidores.Program.Main() en e:\Mis documentos\Visual Studio Projects\ConsigueSeguidores\ConsigueSeguidores\Program.cs:línea 20
   en System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   en System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   en Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   en System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   en System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   en System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   en System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   en System.Threading.ThreadHelper.ThreadStart()

I'm a newbie and do not understand why it gives that error. Can anyone help me?

Apr 4, 2014 at 11:42 PM
Edited Apr 4, 2014 at 11:45 PM

I really don't know how this error can happen as the JObjectStaticWrapper.GetJobjectFromJson is null protected and the JObject.Parse uses the json value...

Would you please tell me which version of Tweetinvi you are using?
I might need to need you to send me your code so that I can investigate this problem.

Marked as answer by linvi on 4/5/2014 at 6:37 AM
Apr 5, 2014 at 3:12 AM
Hello again. Thanks for your quick reply.

Tweetinvi.dll file version I was using was I downloaded the latest and it works correctly.

Thank you very much for your attention.

May 22, 2014 at 3:40 PM
Edited May 22, 2014 at 3:44 PM
I am trying the sample code above in order to follow list of twitter users. But its doing going through, i.e "user.FollowUser()" is always "FALSE".

Here is my sample code:
TwitterCredentials.ApplicationCredentials = TwitterCredentials.CreateCredentials(

var loggeduser = User.GetLoggedUser(TwitterCredentials.ApplicationCredentials);

foreach (string id in _userIds)
        TwitterCredentials.ApplicationCredentials, () =>
            var user = User.GetUserFromScreenName(id);
            if (loggeduser.FollowUser(user))
                Console.WriteLine("Now following @" + user.ScreenName);
Result: None of the users are being followed from the list (provided that "user" information is always retrieved correctly from Twitter).

My questions:
  1. Is there anything wrong in above code snippet ?
  2. Is there some kind of approval required from the "user" being following up ? I mean, can my application listen to the activities of "user" instantly after "FollowUser" succeed ? And will "loggeduser" be listed under "user"'s Followers list on Twitter website ?
  3. How can I check that a particular user is already in my Followings list ? Something like "if (loggeduser.Followings.contains(user.ID)) { ... } "
May 26, 2014 at 8:19 PM

I have not been around in a while so sorry for not replying earlier.
Your code looks perfectly correct. This error might result from an exception in relation with your credentials.

Would you please check the last exception and send me the information back.
// Get the last exception occured
 var lastException = ExceptionHandler.GetLastException();
May 27, 2014 at 2:49 PM
Hi Linvi,

Please find the attached screenshot showing the error details.


May 28, 2014 at 8:13 PM

I cannot access the attached documents.
Would you please try again to send it?

May 29, 2014 at 9:20 AM
File re-attached.

Thanks and regards.