【问题标题】:Publishing tweets from C# Windows service using Tweetinvi or similar使用 Tweetinvi 或类似工具从 C# Windows 服务发布推文
【发布时间】:2018-02-20 11:11:54
【问题描述】:

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

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

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

当然,我可以执行此处提到的基于 PIN 的身份验证:

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

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

当然,我将拥有用于发布状态的 twitter 帐户的密码,但我看不到无需手动用户干预即可进行良好的老式登录的方法 - 我有哪些选择?

【问题讨论】:

    标签: c# authentication twitter tweetinvi


    【解决方案1】:

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

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

    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 中提取值的 cmets。将它们保存在您的数据库中,然后在您的服务中检索它们以代表用户拨打电话。

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

    【讨论】:

    • 谢谢,乔。我接受了你的回答,因为它让我走上了正轨!
    猜你喜欢
    • 2021-08-03
    • 2017-05-30
    • 2016-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-08
    • 1970-01-01
    • 2013-04-05
    相关资源
    最近更新 更多