LinqToTwitter状态更新有时仅适用

时间:2014-05-06 19:52:02

标签: c# .net twitter async-await linq-to-twitter

我正在尝试使用LinqToTwitter发送推文。我的问题是它几乎永远不会奏效。我已经让它工作了三十次中的三次,通常那是我尝试调试的时候。我认为这可能与时间和身份验证有关,但我不知道该怎么做。代码运行没有错误,但没有生成推文。

public static async Task SendTweetAsync(string text)
    {
        // Get the authorization credentials
        var auth = GetCredentials();

        // Create the twitter context
        var ctx = new TwitterContext(auth);

        try
        {
            Status responseTweet = await ctx.TweetAsync(text);
        }
        catch (Exception e) {
            throw e;
        }
    }

    private static AspNetAuthorizer GetCredentials()
    {
        return new AspNetAuthorizer
        {
            CredentialStore = new InMemoryCredentialStore()
            {
                ConsumerSecret = "##########",
                ConsumerKey = "##########",
                OAuthToken = "##########",
                OAuthTokenSecret = "##########",
                UserID = ##########
            }
        };
    }

1 个答案:

答案 0 :(得分:0)

我不知道@JoeMayo之前的答案发生了什么,但对我有用的是将AspNetAuthorizer更改为SingleUserAuthorizer

private static SingleUserAuthorizer GetCredentials()
{
    return new SingleUserAuthorizer
    {
        CredentialStore = new InMemoryCredentialStore()
        {
            ConsumerSecret = "##########",
            ConsumerKey = "##########",
            OAuthToken = "##########",
            OAuthTokenSecret = "##########",
            UserID = ##########
        }
    };
}

另外,我添加了await auth.AuthorizeAsync();

var auth = GetCredentials();

await auth.AuthorizeAsync();

正如我所说,应该归功于@JoeMayo,但他的回答已经消失。