无需每次重新验证即可保存 Tweetinvi 凭据

Saving Tweetinvi credentials without re-authenticating every time

基本上我想做的是从用户那里获取最近的推文并用它们做一些事情。我正在使用基于 PIN 身份验证的 Tweetinvi,如网站上所述,如下所示:

// Create a new set of credentials for the application
var appCredentials = new TwitterCredentials("CONSUMER_KEY", "CONSUMER_SECRET");

// Go to the URL so that Twitter authenticates the user and gives him a PIN code
var url = CredentialsCreator.GetAuthorizationURL(appCredentials);

// This line is an example, on how to make the user go on the URL
Process.Start(url);

// 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 = CredentialsCreator.GetCredentialsFromVerifierCode(pinCode, appCredentials);

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

现在的问题是,每次通过浏览器启动我的应用程序时,我都必须登录并连接到 Twitter,这有点烦人。我试图将我的身份验证详细信息保存在文本文件中(消费者密钥、消费者秘密、访问令牌、访问令牌秘密),然后将信息插入 appCredentials 和 userCredentials,但没有结果,因为我不断收到 TwitterNullCredentialsException。那么我该如何保存我的凭据,这样我就不必在每次启动时都重新连接?

我是Tweetinvi的主要开发者。

如果您存储了 4 个凭据信息,则可以通过 2 个不同的解决方案重复使用它们:

Auth.SetUserCredentials("CONSUMER_KEY", "CONSUMER_SECRET", "ACCESS_TOKEN", "ACCESS_TOKEN_SECRET");
// OR
var creds = new TwitterCredentials("CONSUMER_KEY", "CONSUMER_SECRET", "ACCESS_TOKEN", "ACCESS_TOKEN_SECRET");
Auth.SetCredentials(creds);

文档可能会帮助您设置应用程序:https://github.com/linvi/tweetinvi/wiki/Introduction