【问题标题】:Linq-To-Twitter Bad AuthenticationLinq-To-Twitter 错误身份验证
【发布时间】:2013-01-30 16:27:45
【问题描述】:

所以我一直在使用 Linq-To-Twitter 将 Twitter 集成添加到我的 Windows 8 应用商店应用程序中,Moreso 用于玩弄它,但我遇到了一个问题。我当前的身份验证代码块是

  var auth = new WinRtAuthorizer
        {
            Credentials = new LocalDataCredentials
            {
                ConsumerKey = "",
                ConsumerSecret = ""
            },
            UseCompression = true,
            Callback = new Uri("http://linqtotwitter.codeplex.com/")
        };

        if (auth == null || !auth.IsAuthorized)
        {
            await auth.AuthorizeAsync();
        }

这很好用,除非我进入身份验证屏幕并单击左上角的后退按钮以退出身份验证而不提供详细信息。此时我得到一个 TwitterQueryException: Bad Authentication data at:

                var timelineResponse =
                 (from tweet in twitterCtx.Status
                  where tweet.Type == StatusType.Home
                  select tweet)
                 .ToList();

显然,因为身份验证信息错误,我正在尝试找到一种方法,以在身份验证失败/退出时停止继续执行其余代码。

我尝试过简单的布尔检查,但没有任何效果。几个小时以来,我一直在融化我的大脑,所以任何帮助都将不胜感激。非常感谢!

【问题讨论】:

    标签: c# windows-8 microsoft-metro windows-store-apps linq-to-twitter


    【解决方案1】:

    您可以查询 Account.VerifyCredentials 以确保用户在执行任何其他操作之前已登录。这是一个例子:

            const int BadAuthenticationData = 215;
    
            var twitterCtx = new TwitterContext(auth);
    
            try
            {
                var account =
                    (from acct in twitterCtx.Account
                     where acct.Type == AccountType.VerifyCredentials
                     select acct)
                    .SingleOrDefault();
    
                await new MessageDialog(
                    "Screen Name: " + account.User.Identifier.ScreenName, 
                    "Verification Passed")
                    .ShowAsync();
            }
            catch (TwitterQueryException tqEx)
            {
                if (tqEx.ErrorCode == BadAuthenticationData)
                {
                    new MessageDialog(
                        "User not authenticated", 
                        "Error During Verification.")
                        .ShowAsync();
                    return;
                }
    
                throw;
            }
    

    您的错误处理策略将与此不同,这只是一个示例,但它向您展示了如何知道发生了错误并让您有机会在恢复正常操作之前对问题做出反应。

    TwitterQueryException 将在 ErrorCode 属性中包含 Twitter 错误代码。它还将 Message 设置为 Twitter 返回的错误消息。 InnerException 提供带有原始堆栈跟踪的底层异常,这通常是由于 Twitter 返回的 HTTP 错误代码而引发的 WebException。

    【讨论】:

    • 太棒了!这正是我需要的,非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-01
    • 2011-08-04
    • 2016-07-26
    • 2019-05-06
    • 2012-11-04
    • 2015-03-22
    相关资源
    最近更新 更多