2

i am trying to learn connecting C# to Twitter by using tweetinvi.

i have no problem while connecting with Twitter Key and Twitter Token

then i debug my code, noticed null value on User.GetAuthenticatedUser()

however, i'm already authorize the twitter apps with my own twitter account.

Why does User.GetAuthenticatedUser() return Null Value ?

i got the following picture while trying to pass the error into Message Box enter image description here

how do i resolve this ?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tweetinvi;


namespace Twitdesk
{
    public partial class Form1 : Form
    {
        Cl_Tweetinvi twitinvi;
        Command cmd = new Command();

        public Form1()
        {
            InitializeComponent();
            twitinvi = new Cl_Tweetinvi();
            var AuthenticatedUser = User.GetAuthenticatedUser();
            if(AuthenticatedUser == null)
            {
                var latestException = ExceptionHandler.GetLastException();
                MessageBox.Show(latestException.ToString());
                Application.Exit();
            }
            else
            {
                var settings = AuthenticatedUser.GetAccountSettings();
            }

            var tweets = Timeline.GetHomeTimeline();            
            this.Text = cmd.title;
            MessageBox.Show("done");
        }
    }
}

enter image description here

Cignitor
  • 891
  • 3
  • 16
  • 36

1 Answers1

1

The problem comes from the fact that you have not initialized your credentials.

You need to call Auth.SetUserCredentials("CONSUMER_KEY", "CONSUMER_SECRET", "ACCESS_TOKEN", "ACCESS_TOKEN_SECRET"); before performing any operation.

When you have invoked this line any operation will be using these credentials.

Please take a quick look at the wiki for more information. Or let me know if you still encounter any problem.

Though the problem could be different as the error message seems to indicate that you have a timeout problem. Normally authentication problems return 401 exception.

Linvi
  • 2,077
  • 1
  • 14
  • 28
  • hi, i made some update and your code was already written. i was wondering why `AuthenticatedUser` become `null` – Cignitor Apr 23 '16 at 00:55
  • nope, i don't know why `AuthenticatedUser` return `Null`, because there's my own twitter account using my twitter apps. – Cignitor Apr 23 '16 at 09:56
  • `AuthenticatedUser` returns null because Twitter is returning an exception. The default behavior of Tweetinvi is to return null when an query fails. – Linvi Apr 23 '16 at 19:11
  • I am going to investigate further what could cause a Timeout exception from Twitter. Is your internet connection stable? – Linvi Apr 23 '16 at 19:12
  • Do you have the elevated privileges required to get the email included? (`include_email=true`). – Linvi Apr 23 '16 at 19:15
  • I have found out that the problem comes from the fact that you are not able to retrieve a response on time from Twitter. But what I do not understand is the fact that your Timeout is set to 10 seconds which should be more than enough to perform such a request. Please let me know if your internet connection might have some problems. Whether you need a proxy, vpn... – Linvi Apr 23 '16 at 19:19
  • yep, my internet connection was stable. and still i don't know if there's something wrong with my code, or my twitter application. i didn't set my Timout into 10seconds. should i change the value now ? – Cignitor Apr 24 '16 at 08:16
  • I don't think you should change the default settings. Could you try to go on https://apigee.com/console/twitter and see if this is working properly for you. – Linvi Apr 24 '16 at 21:26
  • I will be happy to help you solve your problem but I think that we will have to have a live session in which I would be able to see your the application running in live. If you are happy with this please post a message on https://gitter.im/linvi/tweetinvi and I will reply to you privately. – Linvi Apr 24 '16 at 22:46