【问题标题】:Integrate Twitter in a website将 Twitter 集成到网站中
【发布时间】:2010-08-11 23:47:06
【问题描述】:

我正在尝试让一个网站连接到一个 Twitter 帐户,以便我可以在我的网站上显示这些推文。当您通过 OAuth 身份验证连接时,我可以让应用程序工作,它会询问我是否要允许该应用程序。

我想做的是因为这是我自己的 Twitter 帐户,我希望能够登录而不必每次都这样做。我希望网站发送我的凭据,以便用户只看到页面,并可以查看推文。这可能吗?

【问题讨论】:

  • 您需要登录吗?你的推文不是公开的吗?我意识到您通过登录可以获得更好的请求率,但无论如何您都应该将它们缓存在服务器端并且不经常刷新。
  • @Rup:我需要登录才能显示受保护的推文。
  • 好的,这使我的回复无效:)

标签: c# twitter oauth


【解决方案1】:

如果您的帐户没有受到保护,您可以使用以下公共时间线:

http://twitter.com/statuses/user_timeline/mytwittername.json?callback=twitterCallback2&count=4

这将为您提供最后 4 条推文,还有其他方法可以在没有身份验证的情况下检索您的推文

【讨论】:

    【解决方案2】:

    我已经想通了。我创建了一个继承 IConsumerTokenManager 接口的新类。然后,我需要更改的唯一属性是ConsumerKeyConsumerSecretGetTokenSecret(如下所示)。

    #region Namespaces
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using DotNetOpenAuth.OAuth.ChannelElements;
    using DotNetOpenAuth.OAuth.Messages; 
    #endregion
    
    namespace BingMapTest
    {
        public class ConsumerTokenManager : IConsumerTokenManager
        {
            public string ConsumerKey
            {
                get { return "xxxxxxxx"; }
            }
    
            public string ConsumerSecret
            {
                get { return "xxxxxxxx"; }
            }
    
            public string GetTokenSecret(string token)
            {
                return "xxxxxxxx";
            }
    
            public void ExpireRequestTokenAndStoreNewAccessToken(string consumerKey, string requestToken, string accessToken, string accessTokenSecret)
            {
                throw new NotImplementedException();
            }
    
            public TokenType GetTokenType(string token)
            {
                throw new NotImplementedException();
            }
    
            public bool IsRequestTokenAuthorized(string requestToken)
            {
                throw new NotImplementedException();
            }
    
            public void StoreNewRequestToken(UnauthorizedTokenRequest request, ITokenSecretContainingMessage response)
            {
                throw new NotImplementedException();
            }
        }
    }
    

    然后,回到我的 Page_Load 方法中,我实例化新的 ConsumerTokenManager

    protected void Page_Load(object sender, EventArgs e)
            {
                ITwitterAuthorization auth;
    
                ConsumerTokenManager tokenManager = new ConsumerTokenManager();
    
                auth = new WebOAuthAuthorization(tokenManager, "accessToken");
                auth.UseCompression = true;
    
                // For Twitter
                using (var twitterCtx = new TwitterContext(auth, "https://api.twitter.com/1/", "https://search.twitter.com/"))
                {                   
                    // Whatever authorization module we selected... sign on now.  
                    try
                    {
                        auth.SignOn();
                    }
                    catch (OperationCanceledException)
                    {
                        return;
                    }
                }
            }
    

    如果其他人有类似的问题,我想我会分享。

    【讨论】:

      【解决方案3】:

      您仍然必须使用 oauth。

      这个想法是,当您登录时,任何 oauth API 都会返回一个访问令牌。有时需要定期重新请求访问令牌,但在 Twitter 的情况下,the FAQ states(几乎在底部)它们不会使它们过期。

      此访问令牌用于签署所有未来调用 - 例如检索推文等。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-05-05
        • 1970-01-01
        • 1970-01-01
        • 2016-10-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多