使用 Tweetinvi 或类似工具从 C# Windows 服务发布推文

Publishing tweets from C# Windows service using Tweetinvi or similar

我正在考虑使用 Tweetinvi 在 Twitter 上发布一些服务状态更新,这似乎是一个做这类事情的好库,但我才刚刚开始研究这个,所以使用它并不是一成不变的。

但是,我的研究还没有得出一件事,那就是在本质上是无头服务的情况下处理 Twitter 身份验证的明显方法。我已经用 Twitter 创建了一个应用程序,所以我有我的消费者密钥和秘密,我可以执行 "app only" 身份验证来请求用户信息、获得他们的关注者等,但当然我无权发布推文。

所以我的目标是(一旦它结束测试版)创建一个合适的 Twitter 帐户,以某种方式让服务对该帐户进行身份验证,然后按规定的时间间隔从一般服务发布状态更新。这是一个相当简单的想法。

当然,我可以做一些像这里提到的基于 PIN 的身份验证:

https://github.com/linvi/tweetinvi/wiki/Authentication

我可以 运行 手动获取 PIN 码,然后继续工作流程。但这是否需要定期重新认证,还是基本上有效"forever"?我正在寻找一种方法使它尽可能自动化,并且必须每隔 x 小时重做一次身份验证,如果不是阻碍的话,这对这个梦想来说是一个巨大的打击。

当然,我会得到用于发布状态的 Twitter 帐户的密码,但我看不出有什么方法可以在没有用户手动干预的情况下进行良好的老式登录 - 我有什么选择?

此行为是设计使然。 Twitter 使用 OAuth,这是一种允许用户授权应用程序的协议。这对用户有好处,因为否则,您或其他任何人都可以在他们不知情的情况下代表他们执行操作。

考虑到这一点,唯一的方法就是让用户明确授权您的应用。这是我使用 ASP.NET MVC 编写的 LINQ to Twitter 的示例。当用户访问您的页面时,您可以使用一个按钮将他们重定向到 OAuthController 下面的 BeginAsync 操作。

using System;
using System.Configuration;
using System.Linq;
using System.Threading.Tasks;
using System.Web.Mvc;
using LinqToTwitter;

namespace MvcDemo.Controllers
{
    public class OAuthController : AsyncController
    {
        public ActionResult Index()
        {
            return View();
        }

        public async Task<ActionResult> BeginAsync()
        {
            //var auth = new MvcSignInAuthorizer
            var auth = new MvcAuthorizer
            {
                CredentialStore = new SessionStateCredentialStore
                {
                    ConsumerKey = ConfigurationManager.AppSettings["consumerKey"],
                    ConsumerSecret = ConfigurationManager.AppSettings["consumerSecret"]
                }
            };

            string twitterCallbackUrl = Request.Url.ToString().Replace("Begin", "Complete");
            return await auth.BeginAuthorizationAsync(new Uri(twitterCallbackUrl));
        }

        public async Task<ActionResult> CompleteAsync()
        {
            var auth = new MvcAuthorizer
            {
                CredentialStore = new SessionStateCredentialStore()
            };

            await auth.CompleteAuthorizeAsync(Request.Url);

            // This is how you access credentials after authorization.
            // The oauthToken and oauthTokenSecret do not expire.
            // You can use the userID to associate the credentials with the user.
            // You can save credentials any way you want - database, 
            //   isolated storage, etc. - it's up to you.
            // You can retrieve and load all 4 credentials on subsequent 
            //   queries to avoid the need to re-authorize.
            // When you've loaded all 4 credentials, LINQ to Twitter will let 
            //   you make queries without re-authorizing.
            //
            //var credentials = auth.CredentialStore;
            //string oauthToken = credentials.OAuthToken;
            //string oauthTokenSecret = credentials.OAuthTokenSecret;
            //string screenName = credentials.ScreenName;
            //ulong userID = credentials.UserID;
            //

            return RedirectToAction("Index", "Home");
        }
    }
}

用户授权您的应用程序后,Twitter 会将他们重定向回 CompleteAsync 方法。请注意有关如何从 auth.CredentialStore 中提取值的注释。将它们保存在您的数据库中,然后在您的服务中检索它们以代表用户进行调用。

这些凭据不会更改,但用户可能会在将来的某个时间取消对您的应用程序的授权 - 届时您需要让他们再次授权。您可以在 LINQ to Twitter ASP.NET Samples 页面上获取完整的示例代码。