0

I'm trying to write an app that can tweet using an 'application' I registered with Twitter. I am using TweetSharp and have tried to get my TwitterService set up as follows:

public Twitter(string consumerKey, string consumerSecret)
    {
        this.twitterService = new TwitterService(consumerKey, consumerSecret);
        OAuthRequestToken oAuthRequestToken = this.twitterService.GetRequestToken();
        Uri uri = this.twitterService.GetAuthorizationUri(oAuthRequestToken);
        Process.Start(uri.ToString());
        OAuthAccessToken oAuthAccessToken = 
            this.twitterService.GetAccessToken(oAuthRequestToken);
        this.twitterService
            .AuthenticateWith(oAuthAccessToken.Token, oAuthAccessToken.TokenSecret);
    }

It gets to the OAuthAccessToken line and then takes me to the Authorize [my app] to use your account? page on the Twitter website. Before I specified a phony callback url, it displayed a page with the PIN that my user is supposed to enter when I clicked the 'Authorize app' button. Then when I added a phony callback url, it would attempt to go to that page and my code would blow to smithereens with the following error:

The remote server returned an error: (401) Unauthorized.

What I want to know is: can I tweet programatically without the need to enter a PIN or have a legitimate callback url?

4

1 回答 1

1

Tweets must be sent in the context of a user. (Ref: POST statuses/update.) Therefore, your app must get the user's authorization (an OAuth access token) in order to send a Tweet. Since you can't get an access token without using either PIN-based authentication or a callback URL, I'm afraid that what you are asking simply cannot be done.

但是,如果您只是想避免在用户每次启动您的应用程序时提示他们输入 PIN,那么答案很简单:一旦您拥有有效的访问令牌,将其保存在某个地方(例如,保存到文件中),然后重新加载它下次您的应用程序运行时。对于我的 WinForms 应用程序,我使用 .NET 的内置每用户设置机制来存储访问令牌和访问令牌秘密。Web 应用程序可能会更好地使用数据库或类似的持久访问令牌。

注意:如果您这样做,您还需要检查存储的访问令牌的有效性,如果它不再有效,则重复授权过程。Twitter API 文档建议为此目的使用GET account/verify_credentials方法。

于 2013-05-08T01:36:08.060 回答