Example of Follow/Unfollow

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

Thanks a lot.
Mar 13, 2014 at 2:40 PM
Edited Mar 13, 2014 at 2:40 PM
I believe I have found this information in
https://tweetinvi.codeplex.com/SourceControl/latest#Release/Examplinvi/Program.cs
In
LoggedUser_FollowUser
LoggedUser_UnFollowUser

Thanks for this.
Coordinator
Mar 13, 2014 at 2:46 PM
Hi,

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);
}
Linvi
Marked as answer by linvi on 3/13/2014 at 6:46 AM
Apr 4, 2014 at 4:05 AM
Edited Apr 4, 2014 at 10: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);
            YoUsuario.FollowUser(userToFollow);
        }
    }
but I've the next exception in the line ... YoUsuario.FollowUser(userToFollow):

No se controló System.ArgumentNullException
HResult=-2147467261
Message=El valor no puede ser nulo.
Nombre del parámetro: s
Source=mscorlib
ParamName=s
StackTrace:
   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()
InnerException:

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

Thanks.
Coordinator
Apr 5, 2014 at 12:42 AM
Edited Apr 5, 2014 at 12:45 AM
Hi.

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.

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

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

Thank you very much for your attention.

Regards
May 22, 2014 at 4:40 PM
Edited May 22, 2014 at 4: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(
    Utils.AccessToken,
    Utils.AccessTokenSecret,
    Utils.ConsumerKey,
    Utils.ConsumerSecret);

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

foreach (string id in _userIds)
{
    TwitterCredentials.ExecuteOperationWithCredentials(
        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)) { ... } "
Regards.
Coordinator
May 26, 2014 at 9:19 PM
Hi,

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();
Regards,
Linvi
May 27, 2014 at 3:49 PM
Hi Linvi,

Please find the attached screenshot showing the error details.

Regards.


Coordinator
May 28, 2014 at 9:13 PM
Hi,

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

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

Thanks and regards.