Search code examples
c#tweetinvi

Tweetinvi authenticationContext returns null


Im using Tweetinvi 4.0.3 and .NET 4.7.2. I made a new project and I'm just testing to try and get Twitter working with my desktop application. I'm using the example from the wiki for Tweetinvi but it throws a exception:

System.NullReferenceException: 'Object reference not set to an instance of an object.'

Because authenticationContext is returning null from AuthFlow.InitAuthentication(appCredentials);

Here is my code:

private void Button1_Click(object sender, EventArgs e)
{
    // Create a new set of credentials for the application.
    var appCredentials = new TwitterCredentials(ConsumerKey, ConsumerSecret);

    // Init the authentication process and store the related `AuthenticationContext`.
    var authenticationContext = AuthFlow.InitAuthentication(appCredentials);

    // Go to the URL so that Twitter authenticates the user and gives him a PIN code.
    Process.Start(authenticationContext.AuthorizationURL);

    // Ask the user to enter the pin code given by Twitter
    var pinCode = Console.ReadLine();

    // With this pin code it is now possible to get the credentials back from Twitter
    var userCredentials = AuthFlow.CreateCredentialsFromVerifierCode(pinCode, authenticationContext);

    // Use the user credentials in your application
    Auth.SetCredentials(userCredentials);
}

Any idea what I need to do to make this work? I read something about a callback URL, but it's a desktop application so I don't have a callback URL?


Solution

  • After a large amount of time spent I was able to figure out the issue. I guess Twitter is now requiring TLS 1.2 as stated here: https://api.twitterstat.us/ (scroll down to update on 25/7/2019). Upgrading my .NET Framewokr to 4.6 (from 4.0) resolves the issue.

    I read somewhere else you can use System.Net.ServicePointManager.SecurityProtocol |= System.Net.SecurityProtocolType.Tls12;. Source

    Hope this helps someone else in the future.