问题描述
我使用tweetinvi 4.0.3和.NET 4.7.2。我做了一个新项目,我正在测试以使Twitter与我的桌面应用程序一起使用。我正在使用tweetinvi的Wiki中的示例,但它引发了异常:
System.NullReferenceException:“对象引用未设置为对象的实例。”
因为authenticationContext
从AuthFlow.InitAuthentication(appCredentials);
返回空值
这是我的代码:
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);
}
您知道要完成这项工作需要做什么吗?我读了一些有关回调URL的信息,但这是一个桌面应用程序,所以我没有回调URL?
解决方法
花费大量时间后,我得以找出问题所在。我猜Twitter现在需要TLS 1.2,如此处所述:https://api.twitterstat.us/(向下滚动以在25/7/2019更新)。将我的.NET Framewokr从4.0升级到4.6(可以解决此问题)。
我在其他地方也可以使用System.Net.ServicePointManager.SecurityProtocol |= System.Net.SecurityProtocolType.Tls12;
。 Source
希望这对以后的人有帮助。